//********************************************************************* /// <summary> /// 执行SQl查询,返回第一行的第一列的值 /// </summary> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令内容</param> /// <param name="Tra">事物对象</param> /// <param name="commandParameters">参数对象数组</param> /// <returns>第一行的第一列的值</returns> //********************************************************************* public object ExecuteScalar(CommandType cmdType, string cmdText, IDbTransaction Tra, params IDataParameter[] commandParameters) { if (String.IsNullOrEmpty(cmdText)) { throw new ArgumentNullException("cmdText"); } if (Tra == null) { Tra = ContextTransaction; //内部事务对象优先级较低 } using (IDbCommand cmd = m_DBaseAdapter.DbFactory.CreateCommand()) { if (Tra == null) { IDbConnection dbConnection = m_DBaseAdapter.DbFactory.CreateConnection(); dbConnection.ConnectionString = m_ConnectString; dbConnection.Open(); SetReadIsolationLevel(dbConnection); // cmdText = "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;"+cmdText; DataBaseEntry.PrepareCommand(dbConnection, cmd, cmdType, cmdText, commandParameters); } else { DataBaseEntry.PrepareCommand(Tra.Connection, cmd, cmdType, cmdText, commandParameters); cmd.Transaction = Tra is DataBaseTransaction ? ((DataBaseTransaction)Tra).Tra : Tra; } return(DataBaseEntry.IExecuteScalar(cmd)); } }
//********************************************************************* /// <summary> /// 执行SQL查询,返回成功添加或刷新的行数 /// </summary> /// <param name="cmdType">命令行类型</param> ///<param name="cmdText">命令内容</param> /// <param name="Tra">事物对象</param> /// <param name="commandParameters">参数对象数组</param> /// <returns>返回成功获取的行数</returns> //********************************************************************* public DataSet ExecuteQuery(CommandType cmdType, string cmdText, IDbTransaction Tra, params IDataParameter[] commandParameters) { if (String.IsNullOrEmpty(cmdText)) { throw new ArgumentNullException("cmdText"); } if (Tra == null) { Tra = ContextTransaction; //内部事务对象优先级较低 } using (IDbCommand cmd = m_DBaseAdapter.DbFactory.CreateCommand()) { if (Tra == null) { IDbConnection dbConnection = m_DBaseAdapter.DbFactory.CreateConnection(); dbConnection.ConnectionString = m_ConnectString; //dbConnection.Open();//这里不要打开,避免连接池被用尽 DataBaseEntry.PrepareCommand(dbConnection, cmd, cmdType, cmdText, commandParameters); // SetReadIsolationLevel(dbConnection); } else { DataBaseEntry.PrepareCommand(Tra.Connection, cmd, cmdType, cmdText, commandParameters); cmd.Transaction = Tra is DataBaseTransaction ? ((DataBaseTransaction)Tra).Tra : Tra; } return(ExecuteQuery(cmd)); } }
//********************************************************************* /// <summary>执行非查询的Sql语句,如果传入的TraConnection,和SqlTra /// 不为null,则使用此连接,如果为null则创建新的连接,使用后关闭. /// </summary> /// <param name="sqlCmd">SqlCommand</param> //********************************************************************* public int ExecuteNonQuery(IDbCommand sqlCmd) { if (sqlCmd == null) { throw new ArgumentNullException("sqlCmd"); } return(DataBaseEntry.IExecuteNonQuery(sqlCmd)); }
//******************************************************************** /// <summary> /// 执行带事务的SQl查询 /// </summary> /// <param name="Cmd">带事务的SqlCommand,不能为空</param> /// <returns>成功添加或刷新的行数</returns> //******************************************************************** public DataSet ExecuteQuery(IDbCommand Cmd) { if (Cmd == null) { throw new ArgumentNullException("SqlCmd"); } DataSet ds = null; { IDbDataAdapter dataAdapter = m_DBaseAdapter.DbFactory.CreateDataAdapter(); if (Cmd.Transaction == null && m_DBaseAdapter.GetIsolationLevelSql() != null) { Cmd.CommandText = m_DBaseAdapter.GetIsolationLevelSql() + Cmd.CommandText; } dataAdapter.SelectCommand = Cmd; DataBaseEntry.IExecuteDataAdapter(out ds, dataAdapter); } return(ds); }
/// <summary> /// 执行非查询Procedure /// </summary> /// <param name="StoreProcedureName">存储过程名称</param> /// <param name="outParameters">输出参数名称与值对</param> /// <param name="commandParameters">参数列表</param> /// <returns></returns> public int ExecuteProcedureNonQuery(string StoreProcedureName, out Hashtable outParameters, params IDataParameter[] commandParameters) { outParameters = new Hashtable(); using (IDbCommand cmd = m_DBaseAdapter.DbFactory.CreateCommand()) { IDbConnection dbConnection = m_DBaseAdapter.DbFactory.CreateConnection(); dbConnection.ConnectionString = m_ConnectString; dbConnection.Open(); DataBaseEntry.PrepareCommand(dbConnection, cmd, CommandType.StoredProcedure, StoreProcedureName, commandParameters); int result = ExecuteNonQuery(cmd); foreach (IDataParameter para in cmd.Parameters) { if (para.Direction == ParameterDirection.Output || para.Direction == ParameterDirection.InputOutput) { outParameters.Add(para.ParameterName, para.Value); } } return(result); } }
/// <summary> /// 执行非查询的Sql语句,如果传入的事物对象 /// 不为null,则使用此连接,如果为null则创建新的连接,使用后关闭. /// </summary> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令内容</param> /// <param name="Tra">事物对象</param> /// <param name="commandParameters">参数对象数组</param> /// <returns>影响行数</returns> public int ExecuteNonQuery(CommandType cmdType, string cmdText, IDbTransaction Tra, params IDataParameter[] commandParameters) { if (Tra == null) { Tra = ContextTransaction; //内部事务对象优先级较低 } using (IDbCommand cmd = m_DBaseAdapter.DbFactory.CreateCommand()) { if (Tra == null) { IDbConnection dbConnection = m_DBaseAdapter.DbFactory.CreateConnection(); dbConnection.ConnectionString = m_ConnectString; dbConnection.Open(); DataBaseEntry.PrepareCommand(dbConnection, cmd, cmdType, cmdText, commandParameters); } else { DataBaseEntry.PrepareCommand(Tra.Connection, cmd, cmdType, cmdText, commandParameters); cmd.Transaction = Tra is DataBaseTransaction?((DataBaseTransaction)Tra).Tra:Tra; } return(ExecuteNonQuery(cmd)); } }