View Code
1 string sqlcon = "server =.; database = webSystem; uid = sa; pwd = ";
2 SqlConnection myConnection = new SqlConnection (sqlcon); // create a database connection object
3 myConnection. Open ();
4
5 // start a transaction
6 SqlTransaction sqltrans = myConnection. BeginTransaction ();
7
8 // create a command for the transaction
9 SqlCommand cmd = new SqlCommand ();
10 cmd. Connection = myConnection;
11 cmd. Transaction = sqltrans;
12 try
13 {
14 cmd. CommandText = "update dbo. Sys_LoginUser set UserName = 'allen' where UID = '19 '";
15 cmd. ExecuteNonQuery ();
16 cmd. CommandText = "update dbo. Sys_LoginUser set URealName = 'wankui 'where UID = '19 '";
17 cmd. ExecuteNonQuery ();
18 sqltrans. Commit (); // submit. The commit () method of SqlTransaction must be used to successfully complete a transaction.
19 Response. Write ("two data modified successfully ");
20
21}
22 catch (Exception ex)
23 {
24 sqltrans. Rollback (); // if an error occurs, roll back
25 Response. Write (ex );
26}
27 finally
28 {
29 myConnection. Close (); // Close the database connection
30}
From fangyangwa