62,243
社区成员




using (System.Transactions.TransactionScopetrans trans= new System.Transactions.TransactionScope())
{
try
{
函数1;
函数2;
……
trans.Complete();
}
catch ()
{
trans.Dispose();
}
}
DataAccess da = new DataAccess();
string s = da.ExecuteScalar("select getdate()");
da.Commit();
Response.Writer(s);
public sealed class DataAccess
{
//private static string _appConnectionString = null;
private static string AppConnectionString
{
get
{
return ConfigurationManager.ConnectionStrings["ConnectionSQLSttring"].ConnectionString;
}
}
public DataAccess() : this(false) { }
public DataAccess(bool useTransaction)
{
_useTransaction = useTransaction;
init();
}
private SqlTransaction _t = null;
private SqlConnection _cnn = null;
private bool _disposed = false;
private bool _useTransaction = false;
private void init()
{
SqlConnection cnn = new SqlConnection(DataAccess.AppConnectionString);
cnn.Open();
_cnn = cnn;
if (_useTransaction)
_t = cnn.BeginTransaction(IsolationLevel.Serializable);
}
public void Commit()
{
//if (_disposed)
// throw new ObjectDisposedException(this.ToString());
if (_useTransaction)
{
_t.Commit();
_t = null;
}
_cnn.Close();
_cnn = null;
this.Dispose();
}
private void Dispose()
{
if (!_disposed)
{
if (_cnn != null)
{
if (_t != null)
{
_t.Rollback();
_t = null;
}
_cnn.Close();
_cnn = null;
}
_disposed = true;
GC.SuppressFinalize(this);
}
}
#region GetTable
//[DebuggerStepThrough()]
public DataTable GetTable(string sql)
{
SqlCommand cmd = new SqlCommand(sql);
return GetTable(cmd, "");
}
public DataTable GetTable(string sql, string p_tblName)
{
SqlCommand cmd = new SqlCommand(sql);
return GetTable(cmd, p_tblName);
}
//[DebuggerStepThrough()]
public DataTable GetTable(SqlCommand cmd, string p_tblName)
{
//if (_disposed)
// throw new ObjectDisposedException(this.ToString());
//if (cmd.Connection != null)
// throw new HasConnectionException();
DataTable dt = new DataTable();
if (p_tblName != null)
dt.TableName = p_tblName;
SqlDataAdapter da = null;
try
{
cmd.Connection = _cnn;
cmd.Transaction = _t;
da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
catch
{
throw;
}
finally
{
if (da != null)
da.Dispose();
cmd.Connection = null;
cmd.Transaction = null;
}
return dt;
}
#endregion
#region ExecuteCmd
//[DebuggerStepThrough()]
public int ExecuteCmd(string sql)
{
SqlCommand cmd = new SqlCommand(sql);
return ExecuteCmd(cmd);
}
//[DebuggerStepThrough()]
public int ExecuteCmd(SqlCommand cmd)
{
//if (_disposed)
// throw new ObjectDisposedException(this.ToString());
//if (cmd.Connection != null)
// throw new HasConnectionException();
try
{
cmd.Connection = _cnn;
cmd.Transaction = _t;
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cmd.Connection = null;
cmd.Transaction = null;
}
}
#endregion
#region ExecuteScalar
// [DebuggerStepThrough()]
public object ExecuteScalar(string sql)
{
SqlCommand cmd = new SqlCommand(sql);
return ExecuteScalar(cmd);
}
// [DebuggerStepThrough()]
public object ExecuteScalar(SqlCommand cmd)
{
//if (_disposed)
// throw new ObjectDisposedException(this.ToString());
//if (cmd.Connection != null)
// throw new HasConnectionException();
try
{
cmd.Connection = _cnn;
cmd.Transaction = _t;
return cmd.ExecuteScalar();
}
catch
{
throw;
}
finally
{
cmd.Connection = null;
cmd.Transaction = null;
}
}
#endregion
#region GetDataReader
// [DebuggerStepThrough()]
public SqlDataReader GetDataReader(string sql)
{
SqlCommand cmd = new SqlCommand(sql);
return GetDataReader(cmd);
}
// [DebuggerStepThrough()]
public SqlDataReader GetDataReader(SqlCommand cmd)
{
//if (_disposed)
// throw new ObjectDisposedException(this.ToString());
//if (cmd.Connection != null)
// throw new HasConnectionException();
try
{
cmd.Connection = _cnn;
cmd.Transaction = _t;
return cmd.ExecuteReader();
}
catch
{
throw;
}
finally
{
;
}
}
#endregion
}