- public class SQLHelper
- {
- #region 数据库操作相关方法
- static string Constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
- #region 执行Insert、Update、Delete数据库操作语句
- /// <summary>
- /// 执行Insert、Update、Delete、等数据库操作语句
- /// </summary>
- /// <param name="sql">要执行的SQL语句</param>
- /// <param name="parameters">执行参数化语句需要的参数</param>
- /// <returns></returns>
- public static int ExecteNonQuery(string sql, params SqlParameter[] parameters)
- {
- using (SqlConnection conn = new SqlConnection(Constr))
- {
- conn.Open();
- using (SqlCommand comm = conn.CreateCommand())
- {
- comm.CommandText = sql;
- foreach (SqlParameter parameter in parameters)
- {
- comm.Parameters.Add(parameter);
- }
- return comm.ExecuteNonQuery();
- }
- }
- }
- #endregion
- #region 返回DataTable结果集的select语句
- /// <summary>
- /// 执行select语句,返回DataTable结果集
- /// </summary>
- /// <param name="sql">要执行的select语句</param>
- /// <param name="parameters">参数化查询所需的参数</param>
- /// <returns></returns>
- public static DataTable DataReader(string sql, params SqlParameter[] parameters)
- {
- using (SqlConnection conn = new SqlConnection(Constr))
- {
- conn.Open();
- using (SqlCommand comm = conn.CreateCommand())
- {
- comm.CommandText = sql;
- foreach (SqlParameter parameter in parameters)
- {
- comm.Parameters.Add(parameter);
- }
- DataSet dataset = new DataSet();
- SqlDataAdapter adapter = new SqlDataAdapter(comm);
- adapter.Fill(dataset);
- DataTable table = dataset.Tables[0];
- return table;
- }
- }
- }
- #endregion
- #region 返回DataSet结果集的select语句
- /// <summary>
- /// 执行select语句,返回DataSet结果集
- /// </summary>
- /// <param name="sql">要执行的select语句</param>
- /// <param name="parameters">参数化查询所需的参数</param>
- /// <returns></returns>
- public static DataSet DataSetReader(string sql, params SqlParameter[] parameters)
- {
- using (SqlConnection conn = new SqlConnection(Constr))
- {
- conn.Open();
- using (SqlCommand comm = conn.CreateCommand())
- {
- comm.CommandText = sql;
- foreach (SqlParameter parameter in parameters)
- {
- comm.Parameters.Add(parameter);
- }
- DataSet dataset = new DataSet();
- SqlDataAdapter adapter = new SqlDataAdapter(comm);
- adapter.Fill(dataset);
- return dataset;
- }
- }
- }
- #endregion
复制代码 |