/// <summary> /// 执行SQL语句返回结果集IDataReader /// </summary> /// <param name="sql">SQL语句</param> /// <returns></returns> public IDataReader GetDataReader(string sql) { IDataReader ireader = null; try { if (IDbConn == null || IDbConn.State == ConnectionState.Closed) { IDbConn = DataBaseFactory.GetIDbConnection(); IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(sql); //IDbComm.CommandText = sql; //IDbComm.CommandType = CommandType.Text; // = IDbcmd.ExecuteReader(); ireader = IDbComm.ExecuteReader((CommandBehavior)((int)CommandBehavior.CloseConnection)); } return(ireader); } catch (Exception e) { this.Close(); return(null); throw new Exception(e.Message); } }
/// <summary> /// 执行SQL语句,并返加影响的行数 /// </summary> /// <param name="sql">SQL语句</param> /// <param name="cmdParms">参数</param> /// <returns></returns> public static DataResult ExecuteNonQuery(string sql, params IDbDataParameter[] cmdParms) { sql = DataBaseFactory.GetSQL(sql); DataResult result = new DataResult(); result.Sql = sql + ";"; result.IDbDataParameter = cmdParms; using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); using (IDbComm = DataBaseFactory.GetIDbCommand()) { try { PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, cmdParms); int rows = IDbComm.ExecuteNonQuery(); IDbConn.Close(); IDbComm.Parameters.Clear(); result.RecordCount = rows; return(result); } catch (Exception E) { IDbConn.Close(); result.RecordCount = 0; result.Error = E.Message; return(result); } finally { IDbConn.Close(); } } } }
/// <summary> /// 执行SQL语句返回结果中上的第一行第一列的值,忽略额外的列和行 /// </summary> /// <param name="sql">SQL语句</param> /// <returns></returns> public object ExecuteScalar(string sql) { using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); using (IDbComm = DataBaseFactory.GetIDbCommand(sql)) { try { //IDbComm.CommandText = sql; //IDbComm.CommandType = CommandType.Text; object rows = IDbComm.ExecuteScalar(); IDbConn.Close(); return(rows); } catch (Exception E) { IDbConn.Close(); return(null); throw new Exception(E.Message); } finally { IDbConn.Close(); } } } }
/// <summary> /// 执行SQL语句返回结果集IDataReader /// </summary> /// <param name="sql">SQL语句</param> /// <param name="cmdParms">参数</param> /// <returns></returns> public static DataResult GetDataReader(string sql, params IDbDataParameter[] cmdParms) { sql = DataBaseFactory.GetSQL(sql); DataResult result = new DataResult(); result.Sql = sql + ";"; result.IDbDataParameter = cmdParms; try { if (IDbConn == null || IDbConn.State == ConnectionState.Closed) { IDbConn = DataBaseFactory.GetIDbConnection(); IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(); PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, cmdParms); // = IDbcmd.ExecuteReader(); result.IDataReader = IDbComm.ExecuteReader((CommandBehavior)((int)CommandBehavior.CloseConnection)); return(result); // IDbComm.Parameters.Clear(); } else { return(result); } } catch (Exception e) { IDbConn.Close(); result.Error = e.Message; return(result); } }
/// <summary> /// 执行SQL语句返回结果集object /// </summary> /// <param name="sql">SQL语句</param> /// <param name="cmdParms">参数</param> /// <returns></returns> public static object ExecuteScalar(string sql, params IDbDataParameter[] cmdParms) { sql = DataBaseFactory.GetSQL(sql); DataResult result = new DataResult(); result.Sql = sql + ";"; using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); using (IDbComm = DataBaseFactory.GetIDbCommand()) { try { PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, cmdParms); object rows = IDbComm.ExecuteScalar(); IDbConn.Close(); IDbComm.Parameters.Clear(); return(rows); } catch (Exception E) { IDbConn.Close(); return(null); throw new Exception(E.Message); } finally { IDbConn.Close(); } } } }
/// <summary> /// 执行SQL语句返回结果集DataSet /// </summary> /// <param name="sql">SQL语句</param> /// <param name="cmdParms">参数</param> /// <returns></returns> public DataSet GetDataSet(string sql, params IDbDataParameter[] cmdParms) { using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); using (IDbComm = DataBaseFactory.GetIDbCommand()) { try { PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, cmdParms); IDbDataAdapter dataAdapter = DataBaseFactory.GetIDbDataAdapter(); dataAdapter.SelectCommand = IDbComm; DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet); IDbComm.Parameters.Clear(); return(dataSet); } catch (Exception E) { IDbConn.Close(); return(null); throw new Exception(E.Message); } finally { IDbConn.Close(); } } } }
/// <summary> /// 执行SQL语句,并返加影响的行数 /// </summary> /// <param name="sql">SQL语句</param> /// <returns></returns> public int ExecuteNonQuery(string sql) { using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); using (IDbComm = DataBaseFactory.GetIDbCommand(sql)) { try { int rows = IDbComm.ExecuteNonQuery(); IDbConn.Close(); return(rows); } catch (Exception E) { IDbConn.Close(); return(0); throw new Exception(E.Message); } finally { IDbConn.Close(); } } } }
/// <summary> /// 执行SQL语句,并返加影响的行数 /// </summary> /// <param name="sql">SQL语句</param> /// <returns></returns> public static DataResult ExecuteNonQuery(string sql) { DataResult result = new DataResult(); result.Sql = sql + ";"; using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); using (IDbComm = DataBaseFactory.GetIDbCommand(sql)) { try { int rows = IDbComm.ExecuteNonQuery(); IDbConn.Close(); result.RecordCount = rows; } catch (Exception E) { result.Error = E.Message; IDbConn.Close(); result.RecordCount = 0; return(result); } finally { IDbConn.Close(); } } } return(result); }
/// <summary> /// 执行SQL语句返回结果集IDataReader /// </summary> /// <param name="sql">SQL语句</param> /// <param name="cmdParms">参数</param> /// <returns></returns> public IDataReader GetDataReader(string sql, params IDbDataParameter[] cmdParms) { IDataReader ireader = null; try { if (IDbConn == null || IDbConn.State == ConnectionState.Closed) { IDbConn = DataBaseFactory.GetIDbConnection(); IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(); PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, cmdParms); // = IDbcmd.ExecuteReader(); ireader = IDbComm.ExecuteReader((CommandBehavior)((int)CommandBehavior.CloseConnection)); // IDbComm.Parameters.Clear(); } return(ireader); } catch (Exception e) { this.Close(); return(null); throw new Exception(e.Message); } }
/// <summary> /// 执行SQL语句返回结果集DataSet /// </summary> /// <param name="sql">SQL语句</param> /// <returns></returns> public DataSet GetDataSet(string sql) { using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); DataSet dataset = new DataSet(); try { IDbComm = DataBaseFactory.GetIDbCommand(sql); //IDbComm.CommandText = sql; //IDbComm.CommandType = CommandType.Text; IDbAdpter = DataBaseFactory.GetIDbDataAdapter(IDbComm); //IDbAdpter.SelectCommand = IDbComm; IDbAdpter.Fill(dataset); } catch (Exception e) { IDbConn.Close(); throw new Exception(e.Message); } finally { IDbConn.Close(); } return(dataset); } }
/// <summary> /// 执行SQL语句,并返加影响的行数 /// </summary> /// <param name="sql">SQL语句</param> /// <param name="cmdParms">参数</param> /// <returns></returns> public int ExecuteNonQuery(string sql, params IDbDataParameter[] cmdParms) { using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); using (IDbComm = DataBaseFactory.GetIDbCommand()) { try { PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, cmdParms); int rows = IDbComm.ExecuteNonQuery(); IDbConn.Close(); IDbComm.Parameters.Clear(); return(rows); } catch (Exception E) { IDbConn.Close(); return(0); throw new Exception(E.Message); } finally { IDbConn.Close(); } } } }
/// <summary> /// 执行SQL语句返回结果集DataSet /// </summary> /// <param name="sql">SQL语句</param> /// <returns></returns> public static DataResult GetDataSet(string sql) { sql = DataBaseFactory.GetSQL(sql); DataResult result = new DataResult(); result.Sql = sql + ";"; using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); using (IDbComm = DataBaseFactory.GetIDbCommand()) { try { PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, IDbParams); IDbDataAdapter dataAdapter = DataBaseFactory.GetIDbDataAdapter(); dataAdapter.SelectCommand = IDbComm; DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet); IDbComm.Parameters.Clear(); result.DataSet = dataSet; } catch (Exception E) { IDbConn.Close(); result.Error = E.Message; return(result); } finally { IDbConn.Close(); } } } return(result); //using (IDbConn = DataBaseFactory.GetIDbConnection()) //{ // IDbConn.Open(); // DataSet dataset = new DataSet(); // try // { // IDbComm = DataBaseFactory.GetIDbCommand(sql); // //IDbComm.CommandText = sql; // //IDbComm.CommandType = CommandType.Text; // IDbAdpter = DataBaseFactory.GetIDbDataAdapter(IDbComm); // //IDbAdpter.SelectCommand = IDbComm; // IDbAdpter.Fill(dataset); // } // catch (Exception e) // { // IDbConn.Close(); // throw new Exception(e.Message); // } // finally // { // IDbConn.Close(); // } // return dataset; //} }
/// <summary> /// 开始事务 /// </summary> public static IDbConnection BeginTransaction(string connectionString) { IDbConn = DataBaseFactory.GetIDbConnection(connectionString); IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(IDbConn); IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted); IDbComm.Connection = IDbConn; IDbComm.Transaction = IDbTrans; return(IDbConn); }
/// <summary> /// 通过事务执行多条SQL语句 /// </summary> /// <param name="sqlList">SQL语句集</param> /// <returns></returns> public static DataResult ExecuteByTransaction(List <string> sqlList) { DataResult result = new DataResult(); using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(); IDbComm.Connection = IDbConn; IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted);// DataBaseFactory.GetIDbTransaction(); if (IDbTrans != null) { IDbComm.Transaction = IDbTrans; // IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted); } try { for (int i = 0; i < sqlList.Count; i++) { string str = sqlList[i].ToString(); if (str.Trim().Length > 1) { IDbComm.CommandText = str; result.RecordCount += IDbComm.ExecuteNonQuery(); } } IDbTrans.Commit(); return(result); } catch (Exception exception) { IDbTrans.Rollback(); IDbConn.Close(); result.Error = exception.Message; return(result); } finally { IDbConn.Close(); } } }
/// <summary> /// 执行SQL语句返回结果集IDataReader /// </summary> /// <param name="sql">SQL语句</param> /// <returns></returns> public static DataResult GetDataReader(string sql) { sql = DataBaseFactory.GetSQL(sql); DataResult result = new DataResult(); result.Sql = sql + ";"; IDbConn = DataBaseFactory.GetIDbConnection(); IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(IDbConn); try { PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, IDbParams); result.IDataReader = IDbComm.ExecuteReader(); } catch (Exception E) { result.Error = E.Message; return(result); } return(result); // IDataReader ireader = null; // try // { // if (IDbConn == null || IDbConn.State == ConnectionState.Closed) // { // IDbConn = DataBaseFactory.GetIDbConnection(); // IDbConn.Open(); // IDbComm = DataBaseFactory.GetIDbCommand(sql); // //IDbComm.CommandText = sql; // //IDbComm.CommandType = CommandType.Text; // // = IDbcmd.ExecuteReader(); // ireader = IDbComm.ExecuteReader((CommandBehavior)((int)CommandBehavior.CloseConnection)); // } // return ireader; // } //catch (Exception e) // { // IDbConn.Close(); // return null; // throw new Exception(e.Message); // } }
/// <summary> /// 通过事务执行多条SQL语句 /// </summary> /// <param name="sqlList">SQL语句集</param> /// <returns></returns> public bool ExecuteByTransaction(List <string> sqlList) { using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(); IDbComm.Connection = IDbConn; IDbTrans = IDbConn.BeginTransaction();// DataBaseFactory.GetIDbTransaction(); if (IDbTrans != null) { IDbComm.Transaction = IDbTrans; IDbTrans = IDbConn.BeginTransaction(IsolationLevel.ReadCommitted); } try { for (int i = 0; i < sqlList.Count; i++) { string str = sqlList[i].ToString(); if (str.Trim().Length > 1) { IDbComm.CommandText = str; IDbComm.ExecuteNonQuery(); } } IDbTrans.Commit(); return(true); } catch (Exception exception) { IDbTrans.Rollback(); IDbConn.Close(); return(false); throw new Exception(exception.Message); } finally { IDbConn.Close(); } } }
/// <summary> /// 打开数据库连接 /// </summary> public void Open() { if (IDbConn == null || IDbConn.State == ConnectionState.Closed) { IDbConn = DataBaseFactory.GetIDbConnection(); IDbComm = DataBaseFactory.GetIDbCommand(); IDbAdpter = DataBaseFactory.GetIDbDataAdapter(); // IDbTrans = DataBaseFactory.GetIDbTransaction(); } IDbComm.Connection = IDbConn; IDbAdpter.SelectCommand = IDbComm; IDbComm.CommandType = CommandType.Text; try { IDbConn.Open(); } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// 是否存在记录 /// </summary> /// <param name="sql">SQL语句</param> /// <param name="cmdParms">参数</param> /// <returns></returns> public bool ExistsRecord(string sql, params IDbDataParameter[] cmdParms) { IDataReader ireader = null; try { if (IDbConn == null || IDbConn.State == ConnectionState.Closed) { IDbConn = DataBaseFactory.GetIDbConnection(); IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(); PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, cmdParms); ireader = IDbComm.ExecuteReader((CommandBehavior)((int)CommandBehavior.CloseConnection)); if (ireader.Read()) { IDbComm.Parameters.Clear(); ireader.Close(); this.Close(); return(true); } else { this.Close(); return(false); } } else { return(false); } } catch (Exception e) { this.Close(); return(false); throw new Exception(e.Message); } }
public static DataResult ExistsRecord(string sql) { DataResult result = new DataResult(); result.Sql = sql + ";"; IDataReader ireader = null; try { if (IDbConn == null || IDbConn.State == ConnectionState.Closed) { IDbConn = DataBaseFactory.GetIDbConnection(); IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(sql); //IDbComm.CommandText = sql; //IDbComm.CommandType = CommandType.Text; ireader = IDbComm.ExecuteReader(); if (ireader.Read()) { result.RecordCount = 1; } else { result.RecordCount = 0; } } else { result.RecordCount = 1; } } catch (Exception e) { IDbConn.Close(); result.Error = e.Message; return(result); } return(result); }
/// <summary> /// 执行SQL语句返回结果集DataSet /// </summary> /// <param name="sql">SQL语句</param> /// <param name="cmdParms">参数</param> /// <returns></returns> public static DataResult GetDataSet(string sql, params IDbDataParameter[] cmdParms) { sql = DataBaseFactory.GetSQL(sql); DataResult result = new DataResult(); result.Sql = sql + ";"; result.IDbDataParameter = cmdParms; using (IDbConn = DataBaseFactory.GetIDbConnection()) { IDbConn.Open(); using (IDbComm = DataBaseFactory.GetIDbCommand()) { try { PrepareCommand(IDbComm, IDbConn, null, CommandType.Text, sql, cmdParms); IDbDataAdapter dataAdapter = DataBaseFactory.GetIDbDataAdapter(); dataAdapter.SelectCommand = IDbComm; DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet); IDbComm.Parameters.Clear(); result.DataSet = dataSet; } catch (Exception E) { IDbConn.Close(); result.Error = E.Message; return(result); } finally { IDbConn.Close(); } } } return(result); }
public bool ExistsRecord(string sql) { IDataReader ireader = null; try { if (IDbConn == null || IDbConn.State == ConnectionState.Closed) { IDbConn = DataBaseFactory.GetIDbConnection(); IDbConn.Open(); IDbComm = DataBaseFactory.GetIDbCommand(sql); //IDbComm.CommandText = sql; //IDbComm.CommandType = CommandType.Text; ireader = IDbComm.ExecuteReader(); if (ireader.Read()) { return(true); } else { return(false); } } else { return(false); } } catch (Exception e) { this.Close(); return(false); throw new Exception(e.Message); } }