2023年6月21日发(作者:)
C#DBHelper数据库连接帮助类DBHelper 帮助类1.作⽤:将C#应⽤程序与SQL SERVER数据库交互的公共⽅法(增删改查)提取出来,⽅便调⽤。2.要求:C#⼊门级程序员必须熟练掌握编写3.组成:三个公⽤⽅法。①ExecuteNonQuery 增删改②ExecuteScalar 查询返回单⾏单列③ExecuteQuery 查询返回多⾏多列4.代码明细:public class DBHelper { public static string strConn = "Server=01D;uid=CG;pwd=abc.1234;Database=QQChat"; /// /// 增、删、改通⽤⽅法 /// /// SQL 语句 /// 可选参数 /// public static int ExecuteNonQuery(string Sql, params SqlParameter[] paras) { int result = 0; //using ⾃动关闭资源,⾃带异常处理机制 using (SqlConnection conn = new SqlConnection(strConn)) { ();//打开连接 SqlCommand command = new SqlCommand(Sql, conn);//执⾏命令 ge(paras);//加⼊参数 result = eNonQuery(); //执⾏
} return result; } /// /// 查询单⾏当列 /// /// SQL 语句 /// 可选参数 /// public static Object ExecuteScalar(string Sql, params SqlParameter[] paras) { Object result; //using ⾃动关闭资源,⾃带异常处理机制 using (SqlConnection conn = new SqlConnection(strConn)) { ();//打开连接 SqlCommand command = new SqlCommand(Sql, conn);//执⾏命令 ge(paras);//加⼊参数 result = eScalar(); //执⾏
} return result; } /// /// 查询返回多⾏多列;泛型升级未知对象类型;泛型类,泛型⽅法 /// /// SQL 语句 /// 可选参数 /// public static List ExecuteQuery(string Sql, params SqlParameter[] paras) { List list = new List();//创建泛型集合对象 using (SqlConnection conn = new SqlConnection(strConn)) { ();//打开连接 SqlCommand command = new SqlCommand(Sql, conn);//执⾏命令 ge(paras);//加⼊参数 using (SqlDataReader reader = eReader())//执⾏游标 { Type type = typeof(T);//检测类型 //next 往下执⾏ while (()) { //创建对象 T t=(T)Instance(type); foreach(PropertyInfo pi in perties()) //反射 PropertyInfo类获取基类的属性 { ue(t, reader[] is DBNull ? null:reader[]); //ORM 将游标的值赋给对象 } (t); } } } return list; } }}
2023年6月21日发(作者:)
C#DBHelper数据库连接帮助类DBHelper 帮助类1.作⽤:将C#应⽤程序与SQL SERVER数据库交互的公共⽅法(增删改查)提取出来,⽅便调⽤。2.要求:C#⼊门级程序员必须熟练掌握编写3.组成:三个公⽤⽅法。①ExecuteNonQuery 增删改②ExecuteScalar 查询返回单⾏单列③ExecuteQuery 查询返回多⾏多列4.代码明细:public class DBHelper { public static string strConn = "Server=01D;uid=CG;pwd=abc.1234;Database=QQChat"; /// /// 增、删、改通⽤⽅法 /// /// SQL 语句 /// 可选参数 /// public static int ExecuteNonQuery(string Sql, params SqlParameter[] paras) { int result = 0; //using ⾃动关闭资源,⾃带异常处理机制 using (SqlConnection conn = new SqlConnection(strConn)) { ();//打开连接 SqlCommand command = new SqlCommand(Sql, conn);//执⾏命令 ge(paras);//加⼊参数 result = eNonQuery(); //执⾏
} return result; } /// /// 查询单⾏当列 /// /// SQL 语句 /// 可选参数 /// public static Object ExecuteScalar(string Sql, params SqlParameter[] paras) { Object result; //using ⾃动关闭资源,⾃带异常处理机制 using (SqlConnection conn = new SqlConnection(strConn)) { ();//打开连接 SqlCommand command = new SqlCommand(Sql, conn);//执⾏命令 ge(paras);//加⼊参数 result = eScalar(); //执⾏
} return result; } /// /// 查询返回多⾏多列;泛型升级未知对象类型;泛型类,泛型⽅法 /// /// SQL 语句 /// 可选参数 /// public static List ExecuteQuery(string Sql, params SqlParameter[] paras) { List list = new List();//创建泛型集合对象 using (SqlConnection conn = new SqlConnection(strConn)) { ();//打开连接 SqlCommand command = new SqlCommand(Sql, conn);//执⾏命令 ge(paras);//加⼊参数 using (SqlDataReader reader = eReader())//执⾏游标 { Type type = typeof(T);//检测类型 //next 往下执⾏ while (()) { //创建对象 T t=(T)Instance(type); foreach(PropertyInfo pi in perties()) //反射 PropertyInfo类获取基类的属性 { ue(t, reader[] is DBNull ? null:reader[]); //ORM 将游标的值赋给对象 } (t); } } } return list; } }}
发布评论