标签:创建数据库 query switch 执行sql dataset tran config var nsa
/// <summary> /// SqlHelper类 by zy 2017-8-17 /// </summary> public sealed class SqlHelper { //如果项目中只连接了一个数据库 那么可以在此定义一个固定的连接字符串 private static string connectionStr = ConfigurationManager.ConnectionStrings["SQLConnString1"].ConnectionString; #region ExecuteNonQuery /// <summary> /// 执行sql语句 返回受影响行数 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="cmdType">command类型</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> /// 执行sql语句 返回当前插入的记录id(其实是ExecuteScalar转换成id返回) /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="cmdType">command类型</param> /// <param name="cmdText">sql语句</param> /// <param name="returnID">返回id</param> /// <param name="commandParameters">参数</param> /// <returns></returns> public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, out int returnID, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString)) { returnID = 0; PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); returnID = (Int32)cmd.ExecuteScalar(); cmd.Parameters.Clear(); return returnID; } } /// <summary> /// 执行sql语句 返回受影响行数(数据库连接SqlConnection) /// </summary> /// <param name="connection">SqlConnection连接</param> /// <param name="cmdType">command类型</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> /// 执行sql语句 返回受影响行数(事务SqlTransaction) /// </summary> /// <param name="trans">SqlTransaction事务</param> /// <param name="cmdType">command类型</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; } #endregion #region ExecuteScalar /// <summary> /// 执行sql语句 返回结果集第一行第一列 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="cmdType">Command类型</param> /// <param name="cmdText">sql语句</param> /// <param name="commandParameters">参数</param> /// <returns></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> /// 执行sql语句 返回结果集第一行第一列(事务) /// </summary> /// <param name="trans">SqlTransaction事务</param> /// <param name="cmdType">Command类型</param> /// <param name="cmdText">sql语句</param> /// <param name="commandParameters">参数</param> /// <returns></returns> public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters); object val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } /// <summary> /// 执行sql语句 返回结果集第一行第一列(数据库连接) /// </summary> /// <param name="connection">SqlConnection连接</param> /// <param name="cmdType">Command类型</param> /// <param name="cmdText">sql语句</param> /// <param name="commandParameters">参数</param> /// <returns></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; } #endregion #region ExecuteReader /// <summary> /// 执行sql语句 返回reader /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="cmdType">command类型</param> /// <param name="cmdText">sql语句</param> /// <param name="commandParameters">参数</param> /// <returns></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; } } #endregion #region DataSet /// <summary> /// 执行sql语句 返回DataSet结果集(无参数) /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="commandType">command类型</param> /// <param name="commandText">sql语句</param> /// <returns></returns> public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText) { return ExecuteDataset(connectionString, commandType, commandText, (SqlParameter[])null); } /// <summary> /// 执行sql语句 返回DataSet结果集 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="commandType">command类型</param> /// <param name="commandText">sql语句</param> /// <param name="commandParameters">参数</param> /// <returns></returns> public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) { //创建数据库连接 完成后dispose using (SqlConnection cn = new SqlConnection(connectionString)) { cn.Open(); //根据数据库连接 调用具体方法 return ExecuteDataset(cn, commandType, commandText, commandParameters); } } /// <summary> /// 执行sql语句 返回DataSet结果集(数据库连接SqlConnection) /// </summary> /// <param name="connection">SqlConnection连接</param> /// <param name="commandType">command类型</param> /// <param name="commandText">sql语句</param> /// <param name="commandParameters">参数</param> /// <returns></returns> public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) { //创建command SqlCommand cmd = new SqlCommand(); //准备command对象 PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters); //创建SqlDataAdapter和DataSet SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); //把结果集装进dataset da.Fill(ds); //返回 return ds; } #endregion DataSet #region DataTable /// <summary> /// 执行sql语句 返回DataTable结果集(无参数) /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="commandType">command类型</param> /// <param name="commandText">sql语句</param> /// <returns></returns> public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText) { return ExecuteDataTable(connectionString, commandType, commandText, (SqlParameter[])null); } /// <summary> /// 执行sql语句 返回DataTable结果集 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="commandType">command类型</param> /// <param name="commandText">sql语句</param> /// <param name="commandParameters">参数</param> /// <returns></returns> public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) { using (SqlConnection cn = new SqlConnection(connectionString)) { cn.Open(); return ExecuteDataTable(cn, commandType, commandText, commandParameters); } } /// <summary> /// 执行sql语句 返回DataTable结果集 /// </summary> /// <param name="connection">SqlConnection连接</param> /// <param name="commandType">command类型</param> /// <param name="commandText">sql语句</param> /// <param name="commandParameters">参数</param> /// <returns></returns> public static DataTable ExecuteDataTable(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); return dt; } #endregion DataTable #region PrepareCommand /// <summary> /// 准备Command对象 /// </summary> /// <param name="cmd">SqlCommand 对象</param> /// <param name="conn">SqlConnection 对象</param> /// <param name="trans">SqlTransaction 对象</param> /// <param name="cmdType">Command 类型</param> /// <param name="cmdText">sql语句</param> /// <param name="cmdParms">参数</param> private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) { //打开SqlConnection连接 if (conn.State != ConnectionState.Open) conn.Open(); //给Command的Connection CommandText设置值 cmd.Connection = conn; cmd.CommandText = cmdText; //如果是事务 设置事务 if (trans != null) cmd.Transaction = trans; //Command类型 cmd.CommandType = cmdType; //Command参数 if (cmdParms != null) { foreach (SqlParameter parm in cmdParms) { //在此可以操作参数 比如把参数值里的英文逗号全部改为中文逗号 或者其他操作 //switch (parm.SqlDbType) //{ // case SqlDbType.Char: // case SqlDbType.NChar: // case SqlDbType.NText: // case SqlDbType.NVarChar: // case SqlDbType.Text: // case SqlDbType.VarChar: // if (parm.Value != null && parm.Value != DBNull.Value && parm.ParameterName != "@SQLClause") // { // string tmp = parm.Value.ToString(); // tmp = tmp.Replace(",", ","); // tmp = tmp.Replace("‘", "'"); // parm.Value = tmp; // } // break; //} //把参数添加到Command中的Parameters中 cmd.Parameters.Add(parm); } } } #endregion }
整理出一个比较实用的SqlHelper类 满足大多数情况的使用
标签:创建数据库 query switch 执行sql dataset tran config var nsa
原文地址:http://www.cnblogs.com/Alex-zqzy/p/7381756.html