using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data.SqlClient; using System.Data; /// ///SqlHelper 的摘要说明 /// public class SqlHelper { public SqlHelper() { // //TODO: 在此处添加构造函数逻辑 // } public static object ExecuteScalar(string sql, params SqlParameter[] para) { string connStr = ConfigurationManager.ConnectionStrings["connChat"].ConnectionString; SqlConnection conn = new SqlConnection(connStr); SqlCommand comm = new SqlCommand(sql, conn); comm.Parameters.AddRange(para); object ans; conn.Open(); ans = comm.ExecuteScalar(); conn.Close(); return ans; } public static void ExecuteNonQuery(string sql, params SqlParameter[] para) { string connStr = ConfigurationManager.ConnectionStrings["connChat"].ConnectionString; SqlConnection conn = new SqlConnection(connStr); SqlCommand comm = new SqlCommand(sql, conn); comm.Parameters.AddRange(para); conn.Open(); comm.ExecuteNonQuery(); conn.Close(); } public static SqlDataReader GetDateReader(string sql, params SqlParameter[] para) { string connStr = ConfigurationManager.ConnectionStrings["connChat"].ConnectionString; SqlConnection conn = new SqlConnection(connStr); SqlCommand comm = new SqlCommand(sql, conn); comm.Parameters.AddRange(para); conn.Open(); return comm.ExecuteReader(); } public static DataTable GetDataTable(string sql, params SqlParameter[] para) { string connStr = ConfigurationManager.ConnectionStrings["connChat"].ConnectionString; using (SqlConnection conn = new SqlConnection(connStr)) { SqlDataAdapter sda = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable(); sda.SelectCommand.Parameters.AddRange(para); sda.Fill(dt); return dt; } } }