内容不全,以后会添加。
C#与 SQL 链接。
<!--连接数据库字符串--> <connectionStrings> <add name="DBStrConn" connectionString="Data Source=.; initial Catalog=数据库; user id=用户名; password=密码"/> </connectionStrings>
/* * 获取连接数据库字符串 * 此字符串存在App.config应用配置文件中 */ private static string strConnection = ConfigurationManager.ConnectionStrings["DBStrConn"].ConnectionString; //DBStrConn为在App.config中的"name"名
public static int ToInUpDel_ExecuteNonQuery(string sql) { //连接命令 using (SqlConnection conn = new SqlConnection(strConnection)) { conn.Open(); //打开连接 using (SqlCommand cmd = conn.CreateCommand()) { //执行sql语句 cmd.CommandText = sql; //返回受影响的行数 return cmd.ExecuteNonQuery(); } } }
public static int ToSelect_ExecuteScalar(string sql) { using (SqlConnection conn = new SqlConnection(strConnection)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; //返回结果 return (int)cmd.ExecuteScalar(); } } }
public static DataTable ExecuteDataTable(string sql) { using (SqlConnection conn = new SqlConnection(strConnection)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet dataset = new DataSet(); adapter.Fill(dataset); return dataset.Tables[0]; } } }
public static SqlDataReader ExecuteReader(string sql) { SqlConnection conn = new SqlConnection(strConnection); SqlCommand cmd = new SqlCommand(sql,conn); try { conn.Open(); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); //CommandBehavior.CloseConnection 在关闭DataReader的时候自动关闭Connection return reader; } catch(SqlException ex) { throw ex; } }
-------------------------------------------分割线-----------------------------------------------
---------------------------------------以下是修改的--------------------------------------------
-----------------------------------修改时间:2015.3.2----------------------------------------
public static int ExecuteNonQuery(string sql, CommandType cmdType, params SqlParameter[] pms) { using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.CommandType = cmdType; //设置当前执行的是存储过程还是带参数的sql语句 if (pms != null) { cmd.Parameters.AddRange(pms); } conn.Open(); return cmd.ExecuteNonQuery(); } } }
public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] pms) { using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.CommandType = cmdType; if (pms != null) { cmd.Parameters.AddRange(pms); } conn.Open(); return cmd.ExecuteScalar(); } } }
public static SqlDataReader ExecuteReader(string sql, CommandType cmdType, params SqlParameter[] pms) { SqlConnection conn = new SqlConnection(connStr); using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.CommandType = cmdType; if (pms != null) { cmd.Parameters.AddRange(pms); } try { conn.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch { conn.Close(); conn.Dispose(); throw; } } }
public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] pms) { DataTable dt = new DataTable(); using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connStr)) { adapter.SelectCommand.CommandType = cmdType; if (pms != null) { adapter.SelectCommand.Parameters.AddRange(pms); } adapter.Fill(dt); return dt; } }