sql 五大对象的应用
Posted huangliming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 五大对象的应用相关的知识,希望对你有一定的参考价值。
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Text; using System.Collections.Generic; using MyCry; //Summary description for DataBase namespace DAL { public class DataBase : IDisposable { //数据库连接字符串加密 private static MyCry.XorCrypt Xor = new XorCrypt(); public static string sqlConnection = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString; private string _ConnectionString; protected SqlConnection con; public DataBase() { _ConnectionString = sqlConnection; con = new SqlConnection(_ConnectionString); } /// <summary> /// 重载构造涵数 /// </summary> /// <param name="str">数据连接字符串</param> public DataBase(string str) { try { this._ConnectionString = str; con = new SqlConnection(_ConnectionString); } catch (Exception ex) { throw ex; } } /// <summary> /// 打开数据库连接 /// </summary> public void Open() { if (con.State.Equals(ConnectionState.Open)) { con.Close(); con.Dispose(); } if (_ConnectionString == string.Empty) { _ConnectionString = "server=localhost;database=SFPlatForm;"; } else { con.ConnectionString = _ConnectionString; } if (con.State.Equals(ConnectionState.Closed)) { con.Open(); } } /// <summary> /// 释放数据库连接 /// </summary> public void Dispose() { if (con.State.Equals(ConnectionState.Open)) { con.Close(); con.Dispose(); } else if (_ConnectionString != null) { con.Close(); con.Dispose(); } } /// <summary> /// 创建sqlcommand /// </summary> /// <param name="sql">传入的sql</param> /// <returns></returns> public SqlCommand getcmd(string sql) { SqlCommand cmd = getcmd(); cmd.CommandText = sql; return cmd; } /// <summary> /// 创建无参数的sqlcommand /// </summary> /// <returns></returns> public SqlCommand getcmd() { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; Open(); return cmd; } /// <summary> /// 修,普通的sql执行也推荐用绑定参数的形式,防止注入攻击,批量新增更快等优点。 /// </summary> /// <param name="procName">存储过程名称</param> /// <param name="prams">存储过程参数</param> /// <returns></returns> public SqlCommand getcmd(string sql, SqlParameter[] prams) { SqlCommand cmd = getcmd(sql); //不等于空,则需要添加一个参数到cmd中 if (prams != null) { cmd.Parameters.AddRange(prams); } return cmd; } /// <summary> /// 执行sql语句,返回受影响条数 /// </summary> /// <param name="sql"></param> public int getExecuteNonQuery(string sql) { SqlCommand cmd = getcmd(sql); int num = 0; try { num = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw new Exception(sql); } finally { Dispose(); } return num; } /// <summary> /// 2014-03-14 ADD /// 增加事物,执行sql语句 /// 返回受影响条数 /// </summary> /// <param name="sql"></param> public int GetExecuteNonQueryTran(string sql) { Open(); SqlTransaction conTran = con.BeginTransaction(); SqlCommand cmd = null; int num = 0; try { cmd = new SqlCommand(sql, con, conTran); num = cmd.ExecuteNonQuery(); conTran.Commit(); } catch (Exception ex) { num = 0;//失败重置 conTran.Rollback(); throw new Exception(sql); } finally { Dispose(); } return num; } /// <summary> /// 执行事务,完成同步操作 /// </summary> /// <param name="procName">存储过程名称</param> /// <param name="prams">参数</param> /// <param name="procName1">存储过程名称</param> /// <param name="prams1">参数</param> /// <returns></returns> public bool getProcTransaction(string procName, SqlParameter[] prams, string procName1, SqlParameter[] prams1) { bool result = false; SqlCommand cmd = this.getcmd(procName, prams); cmd.CommandType = CommandType.StoredProcedure; using (SqlTransaction conn = con.BeginTransaction()) { try { cmd.Transaction = conn; cmd.ExecuteNonQuery(); conn.Commit(); result = true; } catch (Exception ex) { result = false; conn.Rollback(); } finally { Dispose(); } return result; } } /// <summary> /// 执行事务Sql语句 /// </summary> /// <param name="Sqls">sql语句组</param> /// <returns></returns> public bool getTransaction(List<string> Sqls) { bool result = false; SqlCommand cmd = this.getcmd(); cmd.CommandType = CommandType.StoredProcedure; SqlTransaction conn = con.BeginTransaction(); try { cmd.Transaction = conn; foreach (string sql in Sqls) { cmd.CommandText = sql; cmd.ExecuteNonQuery(); } conn.Commit(); result = true; } catch { conn.Rollback(); } finally { Dispose(); } return result; } /// <summary> /// 执行存储过程 /// </summary> /// <param name="procName"></param> /// <param name="prams"></param> public int getProcExecuteNonQuery(string procName, SqlParameter[] prams) { SqlCommand cmd = getcmd(procName, prams); cmd.CommandType = CommandType.StoredProcedure; int num = 0; try { num = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { Dispose(); } return num; } /// <summary> /// 执行无参数的存储过程 /// </summary> /// <param name="procName">存储过程名称</param> public void getProcExecuteNonQuery(string procName) { getProcExecuteNonQuery(procName, null); } /// <summary> /// 返回SqlDataReader值 /// </summary> /// <param name="sql">sql语句</param> /// <returns></returns> public SqlDataReader getDataReader(string sql) { SqlCommand cmd = getcmd(sql); cmd.CommandTimeout = 300; // 这里单位是秒,设置超时时间为5分钟 SqlDataReader dr = null; try { dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch { dr.Close(); dr.Dispose(); throw new Exception(sql); } return dr; } /// <summary> /// 返回读取存储过程的SqlDataReader值 /// </summary> /// <param name="procName"></param> /// <param name="prams"></param> /// <returns></returns> public SqlDataReader getDataReader(string procName, SqlParameter[] prams) { SqlCommand cmd = getcmd(procName, prams); cmd.CommandType = CommandType.StoredProcedure; SqlDataReader dr = null; try { dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch { dr.Close(); dr.Dispose(); throw new Exception(procName); } return dr; } /// <summary> /// 返回指定的DataSet /// </summary> /// <param name="sql">sql语句</param> /// <param name="ds">DataSet对象</param> /// <returns></returns> public DataSet getDataSet(string sql, SqlParameter[] sqlParams) { try { SqlCommand cmd = getcmd(sql, sqlParams); cmd.CommandTimeout = 300; // 这里单位是秒,设置超时时间为5分钟 SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } catch { throw new Exception(sql); } finally { Dispose(); } } /// <summary> /// 返回DataSet /// </summary> /// <param name="sql">sql</param> /// <returns></returns> public DataSet getDataSet(string sql) { return getDataSet(sql, null); } public DataView getDataView(string sql) { return getDataSet(sql).Tables[0].DefaultView; } /// <summary> /// 执行存储过程返回指定TableName的DataSet, /// </summary> /// <param name="procName"></param> /// <param name="prams"></param> /// <param name="ds"></param> /// <param name="TableName"></param> /// <returns></returns> public DataSet getProcDataSet(string procName, SqlParameter[] prams) { try { SqlCommand cmd = getcmd(procName, prams); cmd.CommandTimeout = 300; // 这里单位是秒,设置超时时间为5分钟 cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } catch { throw new Exception(procName); } finally { Dispose(); } } /// <summary> /// 通过SqlDataReader判断数据是否存在。 /// </summary> /// <param name="sql"></param> /// <returns></returns> public bool ExistDate(string sql) { SqlDataReader dr = getDataReader(sql); if (dr.Read()) { dr.Close(); Dispose(); return true; } else { dr.Close(); Dispose(); return false; } } /// <summary> /// 通过SqlDataReader判断存储过程中的数据是否存在 /// </summary> /// <param name="procName"></param> /// <param name="prams"></param> /// <returns></returns> public bool ExistDate(string procName, SqlParameter[] prams) { SqlDataReader dr = getDataReader(procName, prams); if (dr.Read()) { dr.Close(); Dispose(); return true; } else { dr.Close(); Dispose(); return false; } } /// <summary> /// 能过SqlDataReader判断存储过程中的数据是否存在 /// </summary> /// <param name="procName"></param> /// <returns></returns> public bool ExistPorcDate(string procName) { return ExistDate(procName, null); } } }
以上是关于sql 五大对象的应用的主要内容,如果未能解决你的问题,请参考以下文章