首页 > 代码库 > 自己写的操作sql的公共类
自己写的操作sql的公共类
/* /‘ `\/ `. . .‘ : `. `. \\.‘ , `.` `. `. ,___/|\. `. : . \, .‘./ ‘ ‘\ , ‘ .\ . \_.~ _; ; \/‘. `\ ..._`. : /.. ../ /‘ _._ \. ~ .‘ `\: /‘.‘@ ` .---. `. .‘ : ‘ @ `.\. / ./`.._./ ~ . :\ `. __ .‘ / ( \....‘ `. .‘ /‘ `. /‘‘‘\ .‘ `. / \ : ;‘ .‘ ..:.‘ ; `\; : : : : .‘ : ; :: `\. `\. ; : \.‘ " ‘ ; `. `. \ / s . / `. .‘ ` . `. `\ `. ; /‘ ;___ ; `. `. `. ` ; ;:__..‘ `. `. `. :` ‘: _.‘ .‘ ; : `. `. .\x./-`--...../‘ ; : `. ..-:..-‘ ( : `---‘`. `; : `. `,.. : : `. `. `.___; `. `. `. `; `-.,‘ 女神保佑 永无BUG*/ public static class SQLHelper { private static SqlConnection Con; private static SqlCommand cmd; private static DataTable dt; private static DataSet ds; private static SqlDataAdapter da; private static string sqlstr; private static void SQLHelper() { sqlstr = ""; } /// <summary> /// 执行一个sql语句,返回第一行第一个值 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static object GetFirstValue(string sql) { using (Con = new SqlConnection(sqlstr)) { Con.Open(); cmd = new SqlCommand(sql, Con); return cmd.ExecuteScalar(); } } /// <summary> /// 执行一个sql语句,返回受影响行数 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int GetNonQuery(string sql) { using (Con = new SqlConnection(sqlstr)) { Con.Open(); cmd = new SqlCommand(sql, Con); return cmd.ExecuteNonQuery(); } } /// <summary> /// 离线模式填充datatable并返回,数据少可以用此方法 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataTable GetDataTableA(string sql) { using (Con = new SqlConnection(sqlstr)) { da = new SqlDataAdapter(sql, Con); dt = new DataTable(); da.Fill(dt); return dt; } } /// <summary> /// 根据sql,填充datatable并返回。数据多的时候可以用此方法 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataTable GetDataTableB(string sql) { using (Con = new SqlConnection(sqlstr)) { Con.Open(); cmd = new SqlCommand(sql, Con); dt = new DataTable(); SqlDataReader reader = cmd.ExecuteReader(); dt.Load(reader); Con.Close(); return dt; } } /// <summary> /// 传入sql语句集,返回一个dataset /// </summary> /// <param name="sqllist"></param> /// <returns></returns> public static DataSet GetDataSet(List<string> sqllist) { using (Con = new SqlConnection(sqlstr)) { Con.Open(); ds = new DataSet(); foreach (string c in sqllist) { cmd = new SqlCommand(c, Con); dt = new DataTable(); SqlDataReader reader = cmd.ExecuteReader(); dt.Load(reader); ds.Tables.Add(dt); } Con.Close(); return ds; } } /// <summary> /// 在事务中执行多条sql语句 /// </summary> /// <param name="list"></param> /// <returns></returns> public static bool ExecSQLSList(List<string> list) { using (Con = new SqlConnection(sqlstr)) { Con.Open(); SqlTransaction tran = Con.BeginTransaction(); try { foreach (string sql in list) { cmd = new SqlCommand(sql, Con); cmd.ExecuteNonQuery(); } tran.Commit(); Con.Close(); return true; } catch { tran.Rollback(); return false; } finally { Con.Close(); Con.Dispose(); } } } /// <summary> /// 执行一个不带参数的存储过程,返回datable /// </summary> /// <param name="StoredProcedureName"></param> /// <returns></returns> public static DataTable GetDataTableByStoredProcedure(string StoredProcedureName) { using (Con = new SqlConnection(sqlstr)) { Con.Open(); cmd = new SqlCommand(); cmd.CommandText = StoredProcedureName; cmd.CommandType = CommandType.StoredProcedure; dt = new DataTable(); dt.Load(cmd.ExecuteReader()); return dt; } } }
备用~
自己写的操作sql的公共类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。