add name=ConnStr connectionString=Data Source=.;Initial Catalog=test;User ID=sa;Password=123456/ using System;using System.Configuration;using System.Data;using System.Data.SqlClient;using S...
<add name="ConnStr" connectionString="Data Source=.;Initial Catalog=test;User ID=sa;Password=123456"/>
using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Collections; namespace MyTest.common { public abstract class SqlHelper { public static readonly string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; private static Hashtable m_ParmCache = Hashtable.Synchronized(new Hashtable()); /// <summary> /// depiction:SQL执行无返回结果的操作 /// </summary> /// <param name="<connectionString>"><数据库链接串></param> /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param> /// <param name="<cmdText>"><执行SQL或存储过程名称></param> /// <param name="<commandParameters>"><参数集></param> /// <returns> ///<对方法返回值的说明,该说明必须明确说明返回的值代表什么含义> /// </returns> public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString)) { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } } /// <summary> /// depiction:SQL执行无返回结果的操作 /// </summary> /// <param name="<connection>"><数据库连接></param> /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param> /// <param name="<cmdText>"><执行SQL或存储过程名称></param> /// <param name="<commandParameters>"><参数集></param> /// <returns> ///<对方法返回值的说明,该说明必须明确说明返回的值代表什么含义> /// </returns> public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } /// <summary> /// depiction:SQL执行无返回结果的操作(存在事务操作) /// </summary> /// <param name="<trans>"><事务></param> /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param> /// <param name="<cmdText>"><执行SQL或存储过程名称></param> /// <param name="<commandParameters>"><参数集></param> /// <returns> ///<对方法返回值的说明,该说明必须明确说明返回的值代表什么含义> /// </returns> public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } /// <summary> /// depiction:SQL执行返回返回数据集SqlDataReader /// </summary> /// <param name="<connectionString>"><数据库连接串></param> /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param> /// <param name="<cmdText>"><执行SQL或存储过程名称></param> /// <param name="<commandParameters>"><参数集></param> /// <returns> /// <返回数据集SqlDataReader> /// </returns> public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(connectionString); try { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return rdr; } catch { conn.Close(); throw; } } /// <summary> /// depiction:SQL执行返回值 /// </summary> /// <param name="<connectionString>"><数据库连接串></param> /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param> /// <param name="<cmdText>"><执行SQL或存储过程名称></param> /// <param name="<commandParameters>"><参数集></param> /// <returns> /// <返回数据集SqlDataReader> /// </returns> public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection connection = new SqlConnection(connectionString)) { PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); object val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } } /// <summary> /// depiction:SQL执行返回值 /// </summary> /// <param name="<connection>"><数据库连接></param> /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param> /// <param name="<cmdText>"><执行SQL或存储过程名称></param> /// <param name="<commandParameters>"><参数集></param> /// <returns> /// <返回SQL执行结果> /// </returns> public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); object val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters) { m_ParmCache[cacheKey] = commandParameters; } /// <summary> /// depiction:SQL执行返回DataTable /// </summary> /// <param name="<connectionString>"><数据库连接串></param> /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param> /// <param name="<cmdText>"><执行SQL或存储过程名称></param> /// <param name="<commandParameters>"><参数集></param> /// <returns> /// <返回数据集DataTable> /// </returns> public static DataTable GetTable(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString)) { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.Fill(dt); } return dt; } /// <summary> /// depiction:SQL执行返回DataSet /// </summary> /// <param name="<connectionString>"><数据库连接串></param> /// <param name="<cmdType>"><执行SQL或存储过程 CommandType.Text|CommandType.StoredProcedure></param> /// <param name="<cmdText>"><执行SQL或存储过程名称></param> /// <param name="<commandParameters>"><参数集></param> /// <returns> /// <返回数据集DataSet> /// </returns> public static DataSet GetDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString)) { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.Fill(ds); } return ds; } public static SqlParameter[] GetCachedParameters(string cacheKey) { SqlParameter[] cachedParms = (SqlParameter[])m_ParmCache[cacheKey]; if (cachedParms == null) return null; SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length]; for (int i = 0, j = cachedParms.Length; i < j; i++) clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone(); return clonedParms; } private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = cmdType; if (cmdParms != null) { foreach (SqlParameter parm in cmdParms) cmd.Parameters.Add(parm); } } public static string ConnStr1 { get; set; } } }
沃梦达教程
本文标题为:C#之SqlHelper
基础教程推荐
猜你喜欢
- Unity UGUI 按钮绑定事件的 4 种方式汇总 2023-05-16
- c# – Linq:SqlFunctions.PatIndex vs string.Contains for string comparisson 2023-11-24
- 如何在C#中使用OpenCV(GOCW使用教程) 2023-03-28
- dotnet core linux 接入支付宝H5支付,提示:System.PlatformNotSupportedException","Message":" 2023-09-26
- c# – 使用Silverlight / windows手机式控件设置我的.Net桌面应用程序GUI 2023-09-18
- Unity使用物理引擎实现多旋翼无人机的模拟飞行 2023-04-14
- C# 下载文件 删除文件 写入文本的实例 2022-12-01
- C# 线程同步的方法 2023-03-09
- Unity实现打砖块游戏 2023-06-08
- .Net Core中间件中IApplicationBuilder.Map()的用法 2023-09-28