public static DataSet ExecuteQuery4DataSet(FactoryArgs args, string cmdText, LBDbParameterCollection cmdParms, enQueryTransType transType) { return(ExecuteQuery4DataSet(args, CommandType.Text, cmdText, cmdParms, transType)); }
private static DataSet ExecuteQuery4DataSet(FactoryArgs args, CommandType cmdType, string cmdText, LBDbParameterCollection cmdParms, enQueryTransType transType) { DataSet dsResult = null; if (args.DbConnection == null) { if (transType != enQueryTransType.Non) { #region -- 没有传入 DbConnection,需要事务 -- using (DbConnection conn = Provider.CreateConnection(args.DBName)) { conn.Open(); IsolationLevel level; if (transType == enQueryTransType.Snapshot && Provider.SnapshotSupported(args.DBName)) { level = IsolationLevel.Snapshot; } else { level = IsolationLevel.ReadCommitted; } using (DbTransaction trans = conn.BeginTransaction(level)) { using (DbCommand cmd = Provider.CreateCommand()) { try { // 准备参数 PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms); // 执行 dsResult = new DataSet(); DbDataAdapter adapter = Provider.CreateDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dsResult); // 提交 trans.Commit(); // 取输出参数值 GetParameterOut(cmdParms, cmd.Parameters); return(dsResult); } catch (Exception ex) { try { trans.Rollback(); } catch { } throw ex; } } } } #endregion -- 没有传入 DbConnection,需要事务 -- } else { #region -- 没有传入 DbConnection,不需要事务 -- using (DbConnection conn = Provider.CreateConnection(args.DBName)) { using (DbCommand cmd = Provider.CreateCommand()) { // 准备参数 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms); // 执行 dsResult = new DataSet(); DbDataAdapter adapter = Provider.CreateDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dsResult); // 取输出参数值 GetParameterOut(cmdParms, cmd.Parameters); return(dsResult); } } #endregion -- 没有传入 DbConnection,不需要事务 -- } } else if (args.DbTrans != null) { #region -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 -- using (DbCommand cmd = Provider.CreateCommand()) { // 准备参数 PrepareCommand(cmd, args.DbConnection, args.DbTrans, cmdType, cmdText, cmdParms); // 执行 dsResult = new DataSet(); DbDataAdapter adapter = Provider.CreateDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dsResult); // 取输出参数值 GetParameterOut(cmdParms, cmd.Parameters); return(dsResult); } #endregion -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 -- } else if (transType != enQueryTransType.Non) { #region -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 -- throw new Exception("当需要事务执行时,如果传入 DbConnection,必须同时传入 DbTransaction。"); #endregion -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 -- } else { #region -- 传入 DbConnection,没有传入 DbTrans,不需要事务 -- using (DbCommand cmd = Provider.CreateCommand()) { // 准备参数 PrepareCommand(cmd, args.DbConnection, null, cmdType, cmdText, cmdParms); // 执行 dsResult = new DataSet(); DbDataAdapter adapter = Provider.CreateDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dsResult); // 取输出参数值 GetParameterOut(cmdParms, cmd.Parameters); return(dsResult); } #endregion -- 传入 DbConnection,没有传入 DbTrans,不需要事务 -- } }
public static DataTable ExecuteQuery(FactoryArgs args, string cmdText, enQueryTransType transType) { return(ExecuteQuery(args, cmdText, null, transType)); }
public static DataSet ExecuteQuery4DataSet(DALExecuteArgs args, string cmdText, enQueryTransType transType, params DbParameter[] cmdParms) { return(ExecuteQuery4DataSet(args, CommandType.Text, cmdText, transType, cmdParms)); }
public static DataTable ExecuteQuery(DALExecuteArgs args, string cmdText, enQueryTransType transType) { return(ExecuteQuery(args, cmdText, transType, null)); }
private static DataTable ExecuteQuery(DALExecuteArgs args, CommandType cmdType, string cmdText, enQueryTransType transType, params DbParameter[] cmdParms) { DataTable dtResult = null; if (args.DbConnection == null) { if (transType != enQueryTransType.Non) { #region -- 没有传入 DbConnection,需要事务 -- //using( DbConnection conn = Provider.CreateConnection( args.DBName ) ) using (DbConnection conn = Provider.CreateConnection()) { try { conn.Open(); IsolationLevel level; //if( TS.DW.Setting.SettingDW.TransMode == 0 ) if (iTransMode == 0) { if (transType == enQueryTransType.ReadUncommitted) { level = IsolationLevel.ReadUncommitted; } else if (transType == enQueryTransType.Snapshot && Provider.SnapshotSupported(args.DBName)) { level = IsolationLevel.Snapshot; } else { level = IsolationLevel.ReadCommitted; } } else { level = IsolationLevel.ReadUncommitted; } using (DbTransaction trans = conn.BeginTransaction(level)) { using (DbCommand cmd = Provider.CreateCommand()) { try { // 准备参数 PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms); // 执行 dtResult = new DataTable("Reuslt"); DbDataAdapter adapter = Provider.CreateDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dtResult); // 提交 trans.Commit(); //// 取输出参数值 //GetParameterOut( cmdParms, cmd.Parameters ); return(dtResult); } catch (Exception ex) { try { trans.Rollback(); } catch { } throw ex; } } } } finally { conn.Close(); } } #endregion -- 没有传入 DbConnection,需要事务 -- } else { #region -- 没有传入 DbConnection,不需要事务 -- //using( DbConnection conn = Provider.CreateConnection( args.DBName ) ) using (DbConnection conn = Provider.CreateConnection()) { try { using (DbCommand cmd = Provider.CreateCommand()) { if (cmdType == CommandType.Text) { cmdText = "set transaction isolation level read uncommitted;" + Environment.NewLine + cmdText; } // 准备参数 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms); // 执行 dtResult = new DataTable("Reuslt"); DbDataAdapter adapter = Provider.CreateDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dtResult); //// 取输出参数值 //GetParameterOut( cmdParms, cmd.Parameters ); return(dtResult); } } finally { conn.Close(); } } #endregion -- 没有传入 DbConnection,不需要事务 -- } } else if (args.DbTrans != null) { #region -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 -- using (DbCommand cmd = Provider.CreateCommand()) { // 准备参数 PrepareCommand(cmd, args.DbConnection, args.DbTrans, cmdType, cmdText, cmdParms); // 执行 dtResult = new DataTable("Reuslt"); DbDataAdapter adapter = Provider.CreateDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dtResult); //// 取输出参数值 //GetParameterOut( cmdParms, cmd.Parameters ); return(dtResult); } #endregion -- 同时传入 DbConnection, DbTrans:事务 Commit, Rollback 由外部控制 -- } else if (transType != enQueryTransType.Non) { #region -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 -- //throw new Exception( "当需要事务执行时,如果传入 DbConnection,必须同时传入 DbTransaction。" ); IsolationLevel level; //if( TS.DW.Setting.SettingDW.TransMode == 0 ) if (iTransMode == 0) { if (transType == enQueryTransType.ReadUncommitted) { level = IsolationLevel.ReadUncommitted; } else if (transType == enQueryTransType.Snapshot && Provider.SnapshotSupported(args.DBName)) { level = IsolationLevel.Snapshot; } else { level = IsolationLevel.ReadCommitted; } } else { level = IsolationLevel.ReadUncommitted; } using (DbTransaction trans = args.DbConnection.BeginTransaction(level)) { using (DbCommand cmd = Provider.CreateCommand()) { try { // 准备参数 PrepareCommand(cmd, args.DbConnection, trans, cmdType, cmdText, cmdParms); // 执行 dtResult = new DataTable("Reuslt"); DbDataAdapter adapter = Provider.CreateDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dtResult); // 提交 trans.Commit(); //// 取输出参数值 //GetParameterOut( cmdParms, cmd.Parameters ); return(dtResult); } catch (Exception ex) { try { trans.Rollback(); } catch { } throw ex; } } } #endregion -- 传入 DbConnection,没有传入 DbTrans,但需要事务:报错 -- } else { #region -- 传入 DbConnection,没有传入 DbTrans,不需要事务 -- using (DbCommand cmd = Provider.CreateCommand()) { if (cmdType == CommandType.Text) { cmdText = "set transaction isolation level read uncommitted;" + Environment.NewLine + cmdText; } // 准备参数 PrepareCommand(cmd, args.DbConnection, null, cmdType, cmdText, cmdParms); // 执行 dtResult = new DataTable("Reuslt"); DbDataAdapter adapter = Provider.CreateDataAdapter(); adapter.SelectCommand = cmd; adapter.Fill(dtResult); //// 取输出参数值 //GetParameterOut( cmdParms, cmd.Parameters ); return(dtResult); } #endregion -- 传入 DbConnection,没有传入 DbTrans,不需要事务 -- } }