using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data.SqlClient; using System.Data; namespace MySqlHelper { public class SQLHelper { private static readonly string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; /// <summary> /// 连接数据库 /// </summary> /// <returns></returns> public static SqlConnection CreateConnection() { SqlConnection conn = new SqlConnection(connstr); conn.Open(); return conn; } /// <summary> /// 返回受影响行数 /// </summary> /// <param name="conn"></param> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static int ExecuteNonQuery(SqlConnection conn, string sql, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } /// <summary> /// 按现有连接,返回受影响行数 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = CreateConnection()) { return ExecuteNonQuery(conn,sql,parameters); } } /// <summary> /// 返回第一行第一条数据 /// </summary> /// <param name="conn"></param> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static object ExecuteScalar(SqlConnection conn, string sql, params SqlParameter[] parameters) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } /// <summary> /// 按现有连接,返回第一行第一条数据 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static object ExecuteScalar(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = CreateConnection()) { return ExecuteScalar(conn,sql,parameters); } } /// <summary> /// 返回多条数据 /// </summary> /// <param name="conn"></param> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static DataTable ExecuteReader(SqlConnection conn, string sql, params SqlParameter[] parameters) { DataTable table = new DataTable(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); using (SqlDataReader reader = cmd.ExecuteReader()) { table.Load(reader); } } return table; } /// <summary> /// 按现有连接,返回多条数据 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static DataTable ExecuteReader(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = CreateConnection()) { return ExecuteReader(conn,sql,parameters); } } } }
Web.config配置文件:
<configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <connectionStrings> <add name="connstr" connectionString="server=PC-20140729GKIU;user id=sa;password=update123;database=sy"/> </connectionStrings> </configuration>