SQLHelper.cs
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.Configuration; public class SQLHelper { public static string GetSqlConnectionString() { //return ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; return "Data Source='IP地址';Initial Catalog='数据库';User ID='用户名';Password='密码'"; } //适合增删改操作,返回影响条数 public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(GetSqlConnectionString())) { using (SqlCommand comm = conn.CreateCommand()) { conn.Open(); comm.CommandText = sql; comm.Parameters.AddRange(parameters); return comm.ExecuteNonQuery(); } } } //查询操作,返回查询结果中的第一行第一列的值 public static object ExecuteScalar(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(GetSqlConnectionString())) { using (SqlCommand comm = conn.CreateCommand()) { conn.Open(); comm.CommandText = sql; comm.Parameters.AddRange(parameters); return comm.ExecuteScalar(); } } } //Adapter调整,查询操作,返回DataTable public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters) { using (SqlDataAdapter adapter = new SqlDataAdapter(sql, GetSqlConnectionString())) { DataTable dt = new DataTable(); adapter.SelectCommand.Parameters.AddRange(parameters); adapter.Fill(dt); return dt; } } public static SqlDataReader ExecuteReader(string sqlText, params SqlParameter[] parameters) { //SqlDataReader要求,它读取数据的时候有,它独占它的SqlConnection对象,而且SqlConnection必须是Open状态 SqlConnection conn = new SqlConnection(GetSqlConnectionString());//不要释放连接,因为后面还需要连接打开状态 SqlCommand cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = sqlText; if (parameters != null) { cmd.Parameters.AddRange(parameters); } //CommandBehavior.CloseConnection当SqlDataReader释放的时候,顺便把SqlConnection对象也释放掉 return cmd.ExecuteReader(CommandBehavior.CloseConnection); } public static SqlDataReader ExecuteReader1(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(GetSqlConnectionString()); cmd.CommandTimeout = 1800; // we use a try/catch here because if the method throws an exception we want to // close the connection throw code, because no datareader will exist, hence the // commandBehaviour.CloseConnection will not work try { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return rdr; } catch { conn.Close(); throw; } } 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); } } }
标签:parameters,C#,数据库,cmd,SQLServer,System,SqlConnection,using,conn From: https://www.cnblogs.com/ydc2023/p/16996080.html