2023年6月21日发(作者:)
SqlHelper链接数据库在UI层中的添加
1. 2. //db是名字,是要连接到DBhelper中的 catalog=数据库3. 4. 例如:
// /// 数据库操作/// public class SqlHelper{ /// /// 数据库连接字符串,从配置⽂件读取 /// private static readonly string strConn = tionStrings["sql"].ConnectionString; /// /// 执⾏查询(⾮连接式),将查询结果全部加载到内存中 /// /// SQL语句 /// SQL参数集合 /// 包含查询结果的表 public static DataTable ExecQuery(string sql, params SqlParameter[] parameters) { //using加载完之后⾃动关闭 using (SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, strConn)) { using (DataTable table = new DataTable()) { //判断参数中是否有值 if (parameters != null && () > 0) { //将参数集合加载到SelectCommand ge(parameters); } try { (table); } catch (Exception ex) //执⾏出错,释放资源 { e(); e(); //抛出异常 throw ex; } return table; } } } /// /// 执⾏查询(连接式),逐⾏读取查询结果 /// /// 使⽤完毕后,⼀定不要忘记() /// SQL语句 /// SQL参数集合 /// SqlDataReader对象 public static SqlDataReader ExecReader(string sql, params SqlParameter[] parameters) { //创建连接对象 //【为什么不能使⽤using?】 SqlConnection sqlConnection = new SqlConnection(strConn); using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection)) { //判断参数中是否有值 if (parameters != null && () > 0) { ge(parameters); } try { (); //将Reader与Connection进⾏绑定,关闭Reader的同时释放Connection return eReader(onnection); } catch (Exception ex) //执⾏出错,释放资源 { e(); (); e(); throw ex; } } } /// /// 执⾏⾮查询(增删改)语句 /// /// SQL语句 /// SQL参数集合 /// 返回受影响的⾏数 public static int ExecNonQuery(string sql, params SqlParameter[] parameters) { using (SqlConnection sqlConnection = new SqlConnection(strConn)) { using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection)) { if (parameters != null && () > 0) { ge(parameters); } try { (); //返回受影响的⾏数 return eNonQuery(); } catch (Exception ex) { e(); (); e(); throw ex; } } } } /// /// 执⾏查询,返回查询结果第⼀⾏第⼀列的值 /// /// 常⽤于查询SUM、COUNT等聚合函数的结果 /// SQL语句 /// SQL参数集合 /// 第⼀⾏第⼀列的值 public static object ExecScalar(string sql, params SqlParameter[] parameters) { using (SqlConnection sqlConnection = new SqlConnection(strConn)) { using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection)) { if (parameters != null && () > 0) { ge(parameters); } try { (); return eScalar(); } catch (Exception ex) { e(); (); e(); throw ex; } } } }}
2023年6月21日发(作者:)
SqlHelper链接数据库在UI层中的添加
1. 2. //db是名字,是要连接到DBhelper中的 catalog=数据库3. 4. 例如:
// /// 数据库操作/// public class SqlHelper{ /// /// 数据库连接字符串,从配置⽂件读取 /// private static readonly string strConn = tionStrings["sql"].ConnectionString; /// /// 执⾏查询(⾮连接式),将查询结果全部加载到内存中 /// /// SQL语句 /// SQL参数集合 /// 包含查询结果的表 public static DataTable ExecQuery(string sql, params SqlParameter[] parameters) { //using加载完之后⾃动关闭 using (SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, strConn)) { using (DataTable table = new DataTable()) { //判断参数中是否有值 if (parameters != null && () > 0) { //将参数集合加载到SelectCommand ge(parameters); } try { (table); } catch (Exception ex) //执⾏出错,释放资源 { e(); e(); //抛出异常 throw ex; } return table; } } } /// /// 执⾏查询(连接式),逐⾏读取查询结果 /// /// 使⽤完毕后,⼀定不要忘记() /// SQL语句 /// SQL参数集合 /// SqlDataReader对象 public static SqlDataReader ExecReader(string sql, params SqlParameter[] parameters) { //创建连接对象 //【为什么不能使⽤using?】 SqlConnection sqlConnection = new SqlConnection(strConn); using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection)) { //判断参数中是否有值 if (parameters != null && () > 0) { ge(parameters); } try { (); //将Reader与Connection进⾏绑定,关闭Reader的同时释放Connection return eReader(onnection); } catch (Exception ex) //执⾏出错,释放资源 { e(); (); e(); throw ex; } } } /// /// 执⾏⾮查询(增删改)语句 /// /// SQL语句 /// SQL参数集合 /// 返回受影响的⾏数 public static int ExecNonQuery(string sql, params SqlParameter[] parameters) { using (SqlConnection sqlConnection = new SqlConnection(strConn)) { using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection)) { if (parameters != null && () > 0) { ge(parameters); } try { (); //返回受影响的⾏数 return eNonQuery(); } catch (Exception ex) { e(); (); e(); throw ex; } } } } /// /// 执⾏查询,返回查询结果第⼀⾏第⼀列的值 /// /// 常⽤于查询SUM、COUNT等聚合函数的结果 /// SQL语句 /// SQL参数集合 /// 第⼀⾏第⼀列的值 public static object ExecScalar(string sql, params SqlParameter[] parameters) { using (SqlConnection sqlConnection = new SqlConnection(strConn)) { using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection)) { if (parameters != null && () > 0) { ge(parameters); } try { (); return eScalar(); } catch (Exception ex) { e(); (); e(); throw ex; } } } }}
发布评论