c# SQLHelper(for winForm)实现代码

2016-02-19 09:10 7 1 收藏

今天图老师小编给大家展示的是c# SQLHelper(for winForm)实现代码,精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!

【 tulaoshi.com - 编程语言 】

SQLHelper.cs
代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace HelloWinForm.DBUtility
{
class SQLHelper
{
#region 通用方法
// 数据连接池
private SqlConnection con;
/// summary
/// 返回数据库连接字符串
/// /summary
/// returns/returns
public static String GetSqlConnection()
{
String conn = ConfigurationManager.AppSettings["connectionString"].ToString();
return conn;
}
#endregion
#region 执行sql字符串
/// summary
/// 执行不带参数的SQL语句
/// /summary
/// param name="Sqlstr"/param
/// returns/returns
public static int ExecuteSql(String Sqlstr)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}
}
/// summary
/// 执行带参数的SQL语句
/// /summary
/// param name="Sqlstr"SQL语句/param
/// param name="param"参数对象数组/param
/// returns/returns
public static int ExecuteSql(String Sqlstr, SqlParameter[] param)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
cmd.Parameters.AddRange(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}
}
/// summary
/// 返回DataReader
/// /summary
/// param name="Sqlstr"/param
/// returns/returns
public static SqlDataReader ExecuteReader(String Sqlstr)
{
String ConnStr = GetSqlConnection();
SqlConnection conn = new SqlConnection(ConnStr);//返回DataReader时,是不可以用using()的
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);//关闭关联的Connection
}
catch //(Exception ex)
{
return null;
}
}
/// summary
/// 执行SQL语句并返回数据表
/// /summary
/// param name="Sqlstr"SQL语句/param
/// returns/returns
public static DataTable ExecuteDt(String Sqlstr)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
DataTable dt = new DataTable();
conn.Open();
da.Fill(dt);
conn.Close();
return dt;
}
}
/// summary
/// 执行SQL语句并返回DataSet
/// /summary
/// param name="Sqlstr"SQL语句/param
/// returns/returns
public static DataSet ExecuteDs(String Sqlstr)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
return ds;
}
}
#endregion
#region 操作存储过程
/// summary
/// 运行存储过程(已重载)
/// /summary
/// param name="procName"存储过程的名字/param
/// returns存储过程的返回值/returns
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// summary
/// 运行存储过程(已重载)
/// /summary
/// param name="procName"存储过程的名字/param
/// param name="prams"存储过程的输入参数列表/param
/// returns存储过程的返回值/returns
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters[0].Value;
}
/// summary
/// 运行存储过程(已重载)
/// /summary
/// param name="procName"存储过程的名字/param
/// param name="dataReader"结果集/param
public void RunProc(string procName, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, null);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// summary
/// 运行存储过程(已重载)
/// /summary
/// param name="procName"存储过程的名字/param
/// param name="prams"存储过程的输入参数列表/param
/// param name="dataReader"结果集/param
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// summary
/// 创建Command对象用于访问存储过程
/// /summary
/// param name="procName"存储过程的名字/param
/// param name="prams"存储过程的输入参数列表/param
/// returnsCommand对象/returns
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
// 确定连接是打开的
Open();
//command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) );
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
// 添加存储过程的输入参数列表
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// 返回Command对象
return cmd;
}
/// summary
/// 创建输入参数
/// /summary
/// param name="ParamName"参数名/param
/// param name="DbType"参数类型/param
/// param name="Size"参数大小/param
/// param name="Value"参数值/param
/// returns新参数对象/returns
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// summary
/// 创建输出参数
/// /summary
/// param name="ParamName"参数名/param
/// param name="DbType"参数类型/param
/// param name="Size"参数大小/param
/// returns新参数对象/returns
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// summary
/// 创建存储过程参数
/// /summary
/// param name="ParamName"参数名/param
/// param name="DbType"参数类型/param
/// param name="Size"参数大小/param
/// param name="Direction"参数的方向(输入/输出)/param
/// param name="Value"参数值/param
/// returns新参数对象/returns
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (Size 0)
{
param = new SqlParameter(ParamName, DbType, Size);
}
else
{
param = new SqlParameter(ParamName, DbType);
}
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
{
param.Value = Value;
}
return param;
}
#endregion
#region 数据库连接和关闭
/// summary
/// 打开连接池
/// /summary
private void Open()
{
// 打开连接池
if (con == null)
{
//这里不仅需要using System.Configuration;还要在引用目录里添加
con = new SqlConnection(GetSqlConnection());
con.Open();
}
}
/// summary
/// 关闭连接池
/// /summary
public void Close()
{
if (con != null)
con.Close();
}
/// summary
/// 释放连接池
/// /summary
public void Dispose()
{
// 确定连接已关闭
if (con != null)
{
con.Dispose();
con = null;
}
}
#endregion
}
}

简单用一下:
代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using HelloWinForm.DBUtility;
namespace HelloWinForm.DAL
{
class Student
{
public string test()
{
string str = "";
SqlDataReader dr = SQLHelper.ExecuteReader("select * from Student");
while (dr.Read())
{
str += dr["StudentNO"].ToString();
}
dr.Close();
return str;
}
}
}

来源:http://www.tulaoshi.com/n/20160219/1589837.html

延伸阅读
摘要  想必大家对小榕时光等扫描器都非常熟悉了,有没有自己写一个的冲动。最近微软推实施了.NET战略方案,C#是主推语言,你们是否有兴趣用C#来实现对局域网IP地址的扫描,尝试一下自己写的快乐,那么请跟我来。  正文  1.先介绍一下使用的类:  DNS类:在.net中的System.net命名空间下,主要的功能是从 Internet&...
1. 概述 有时候我们需要在web页面上显示一张图,比如说一张地图,而这张地图会比较大。这时候如果我们把一张大图分隔成一组小图,那么客户端的显示速度会明显地感觉块。希望阅读本文对你有所帮助。 2. 实现思路 .NET Framework GDI+ 为我们提供了一组丰富地类来编辑图形图像。有关.NET Framework GDI+的...
using System;using System.DirectoryServices; //要增加此DLL文件 private void button3_Click(object sender, System.EventArgs e) { try { DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); DirectoryEntry NewUser = AD.Children.Add("TestUser1", "use...
标签: Web开发
解析得到的代码能通过XHTML 1.0 STRICT验证; 包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.  Ubb.ReadMe.htm UBB代码说明标题[h1]标题一[/h1] 标题一 [h2]标题二[/h2] 标题二 [h1]标题三[/h1] 标题三 [h4]标题四[/h4] 标题四 [h5]标题五[/h5] 标题五 [h6]标题六[/h6] 标题六 链接[url]www.unibetter.co...
/*  FTPFactory.cs  Better view with tab space=4  Written by Jaimon Mathew (jaimonmathew@rediffmail.com)  Rolander,Dan (Dan.Rolander@marriott.com) has modified the  download  method to cope with file ...

经验教程

517

收藏

95
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部