using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data.SqlClient; using System.Data; /// <summary> /// SQL帮助器 /// </summary> public class SQLHelper { private static string ConnStr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; //增删改(insert、delete、update) public static int DoNonQuery(string sql, List<SqlParameter> ps = null) { using (SqlConnection conn = new SqlConnection(ConnStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); if (ps != null) { cmd.Parameters.AddRange(ps.ToArray()); } int RowsAffectedCount = cmd.ExecuteNonQuery(); conn.Close(); return RowsAffectedCount; } } //增删改(insert、delete、update) public static int DoNonQuery(string sql, params SqlParameter[] ps) { using (SqlConnection conn = new SqlConnection(ConnStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); if (ps != null) { cmd.Parameters.AddRange(ps); } int RowsAffectedCount = cmd.ExecuteNonQuery(); conn.Close(); return RowsAffectedCount; } } //取单行单列对象(insert、select) public static object DoScalar(string sql, List<SqlParameter> ps = null) { using (SqlConnection conn = new SqlConnection(ConnStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); if (ps != null) { cmd.Parameters.AddRange(ps.ToArray()); } object ScalarObj = cmd.ExecuteScalar(); conn.Close(); return ScalarObj; } } //取单行单列对象(insert、select) public static object DoScalar(string sql, params SqlParameter[] ps) { using (SqlConnection conn = new SqlConnection(ConnStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); if (ps != null) { cmd.Parameters.AddRange(ps); } object ScalarObj = cmd.ExecuteScalar(); conn.Close(); return ScalarObj; } } //断开式取DataTable对象(select) public static DataTable DoDataTable(string sql, List<SqlParameter> ps = null) { using (SqlConnection conn = new SqlConnection(ConnStr)) { conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sql, conn); if (ps != null) { da.SelectCommand.Parameters.AddRange(ps.ToArray()); } DataSet ds = new DataSet(); da.Fill(ds); conn.Close(); return ds.Tables[0]; } } //断开式取DataTable对象(select) public static DataTable DoDataTable(string sql, params SqlParameter[] ps) { using (SqlConnection conn = new SqlConnection(ConnStr)) { conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sql, conn); if (ps != null) { da.SelectCommand.Parameters.AddRange(ps); } DataSet ds = new DataSet(); da.Fill(ds); conn.Close(); return ds.Tables[0]; } } /* * 下面两个方法新加,未测试 */ //连接式读取数据(select) public static SqlDataReader DoReader(string sql, List<SqlParameter> ps = null) { using (SqlConnection conn = new SqlConnection(ConnStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); if (ps != null) { cmd.Parameters.AddRange(ps.ToArray()); } SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); conn.Close(); return dr; } } //存储过程式取DataTable对象 public static DataTable DoProcedure(string sql, List<SqlParameter> ps = null) { using (SqlConnection conn = new SqlConnection(ConnStr)) { conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sql, conn); //设定命令类型 da.SelectCommand.CommandType = CommandType.StoredProcedure; if (ps != null) { da.SelectCommand.Parameters.AddRange(ps.ToArray()); } DataSet ds = new DataSet(); da.Fill(ds); conn.Close(); return ds.Tables[0]; } } }