TransactionScope 好东西啊
private void button1_Click(object sender, EventArgs e)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
SqlConnection sqlConn = new SqlConnection("server=local;uid=sa;pwd=pwd;database=db;Min Pool Size=10;");
//SqlConnection sqlConn1 = new SqlConnection("server=local;uid=sa;pwd=pwd;database=db2;Min Pool Size=10;");
sqlConn.Open();
//sqlConn1.Open();
string insertSql = "UPDATE emp SET ip='10.10.10.11' WHERE emp.lc=221105827237";
string updateSql = @"INSERT INTO odtl
( FormCode ,
Operater ,
IP ,
LogTxt ,
LogTime ,
Status ,
LogType ,
LmsLogID ,
LmsGuid
)
VALUES ( 22222222222 , -- FormCode - bigint
'test' , -- Operater - nvarchar(20)
NULL , -- IP - nvarchar(20)
'test' , -- LogTxt - nvarchar(300)
'2011-12-26 06:49:59' , -- LogTime - datetime
NULL , -- Status - nvarchar(20)
'test' , -- LogType - nvarchar(20)
0 , -- LmsLogID - bigint
NULL -- LmsGuid - nvarchar(100)
)";
SqlCommand sqlComm = new SqlCommand(insertSql, sqlConn);
sqlComm.CommandType = System.Data.CommandType.Text;
sqlComm.ExecuteNonQuery();
sqlComm = new SqlCommand(updateSql, sqlConn);
sqlComm.CommandType = System.Data.CommandType.Text;
sqlComm.ExecuteNonQuery();
sqlConn.Close();
//throw new Exception("sets");
scope.Complete();
}
}
catch (Exception)
{
MessageBox.Show("test");
}
}