已ASP.Net为例子,新建一个ASP.NET项目
1、配置Web.config文件
连接字符串:Data Source=.;Initial Catalog=Test1;User Id=sa;Password=123456;
DataSource:资源名称,“.”表示本地地址(127.0.0.1),有时候也会访问不到,可以填上本地电脑名称
Initial Catalog:数据库名
User Id:数据库用户名
Password:数据库用户密码
2、新建一个SqlHelper.cs类
编写之前,需要先添加System.Configuration的引用
public static class SqlHelper { //取连接字符串 public static readonly string connstr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; /// <summary> /// 打开连接 /// </summary> /// <returns></returns> public static SqlConnection OpenConnection() { SqlConnection conn = new SqlConnection(connstr); conn.Open(); return conn; } public static int ExecuteNonQuery(string cmdText,params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); return ExecuteNonQuery(conn, cmdText, parameters); } } public static object ExecuteScalar(string cmdText,params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); return ExecuteScalar(conn, cmdText, parameters); } } public static DataTable ExecuteDataTable(string cmdText,params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); return ExecuteDataTable(conn,cmdText,parameters); } } /// <summary> /// 执行语句,返回受影响的行数 /// </summary> /// <param name="conn">连接</param> /// <param name="cmdText">sql语句</param> /// <param name="parameters">参数</param> /// <returns></returns> public static int ExecuteNonQuery(SqlConnection conn, string cmdText,params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } /// <summary> /// 返回查询结果的第一行第一列 /// </summary> /// <param name="conn">连接</param> /// <param name="cmdText">sql语句</param> /// <param name="parameters">参数</param> /// <returns></returns> public static object ExecuteScalar(SqlConnection conn,string cmdText,params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } /// <summary> /// 返回查询结果集 /// </summary> /// <param name="conn">连接</param> /// <param name="cmdText">sql语句</param> /// <param name="parameters">参数</param> /// <returns></returns> public static DataTable ExecuteDataTable(SqlConnection conn,string cmdText,params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = cmdText; cmd.Parameters.AddRange(parameters); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd) ) { DataTable table = new DataTable(); adapter.Fill(table); return table; } } } /// <summary> /// 存值到数据库NULL处理 /// </summary> /// <param name="value"></param> /// <returns></returns> public static object ToDBValue(this object value) { return value == null ? DBNull.Value : value; } /// <summary> /// 从数据库取数据NULL处理 /// </summary> /// <param name="dbValue"></param> /// <returns></returns> public static object FromDBValue(this object dbValue) { return dbValue == null ? null : dbValue; } }
3、。。。