private bool InsertT1T2(string sT1Name, int nT1ID, string sT2Name)
{
bool _Result = false;
DbConnection _dbConnection = new SqlConnection("data Source=WANGLEI2;database=cheshi;uid=sa;pwd=pass");
_dbConnection.Open();
DbTransaction _CurrentTransaction=_dbConnection.BeginTransaction();
string cmdText = "insert into T1 (T1Name) Values ('" + sT1Name + "')";
_Result = InsertT1(cmdText);
if (_Result)
{
string cmdText2 = "insert into T2 (T1ID,T2Name) Values ('" + nT1ID + "','" + sT2Name + "')";
_Result = InsertT1(cmdText2);
}
if (!_Result)
{
if (_CurrentTransaction != null)
_CurrentTransaction.Rollback();
}
else
{
if (_CurrentTransaction != null)
_CurrentTransaction.Commit();
}
_dbConnection.Close();
return _Result;
}
private bool InsertT1(string cmdText)
{
bool _Result = false;
SqlConnection con = new SqlConnection("data Source=.;database=cheshi;uid=sa;pwd=");
con.Open();
SqlCommand com = new SqlCommand(cmdText, con);
int n = com.ExecuteNonQuery();
con.Close();
if (n > 0)
{
_Result = true;
}
return _Result;
}
这样写算不算是一个事务...如果表T1和T2有一个表里添加数据没有成功...会不会都不添加..也就是说回滚?????请高手指点.. 我现在测试的这样好你不能实现回滚的功能...........应该怎么改??????????????????????