//返回SQL影响的行数 public object ExecuteScalar(DbCommand cmd) { MyTransaction tran = MyTransaction.BeginTransaction(cmd); object ret = null; try { ret = cmd.ExecuteScalar(); tran.Commit(); } catch (Exception e) { tran.Rollback(); return(e.ToString()); } return(ret); }
//返回DataTable对象 public DataTable ExecuteDataTable(DbCommand cmd) { DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbProviderName); DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter(); DataTable dataTable = null; MyTransaction _tran = MyTransaction.BeginTransaction(cmd); dbDataAdapter.SelectCommand = cmd; try { dataTable = new DataTable(); dbDataAdapter.Fill(dataTable); _tran.Commit(); } catch (Exception e) { _tran.Rollback(); dataTable = CreateErrorTable(e.ToString()); } return(dataTable); }
//返回DataSet对象 public DataSet ExecuteDataSet(DbCommand cmd) { DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbProviderName); DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter(); DataSet ds = new DataSet(); MyTransaction tran = MyTransaction.BeginTransaction(cmd); if (dbDataAdapter != null) { dbDataAdapter.SelectCommand = cmd; try { dbDataAdapter.Fill(ds); tran.Commit(); } catch (Exception e) { tran.Rollback(); ds.Tables.Add(CreateErrorTable(e.ToString())); } } return(ds); }