How to do the multiple operations on the Database?? Now, we‘ll use the transaction processing in C# to make it.
For example, there are three database operation sentences, which use C# transaction processing .All sentences won’t be executed, if there is wrong with any sentences below:
C# code
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCnn;
sqlCmd.Transaction = sqlCnn.BeginTransaction(); /*The first-phase preparations for the transaction processing have been finished*/
try
{
//write the data the user login with to the database.
string sqlCmd1 = “insert into User_Login (user_id,user_pwd) values(’” + User_ID + “‘,’” + User_PWD + “‘)”;
string sqlCmd2 = “insert into User_Data_Basic values(’” + User_ID + “‘,’” + User_Name + “‘,’” + User_Sex + “‘,” + User_Age_Year + “,” + User_Age_Month + “,” + User_Age_Day + “,”,”,”,”,”)”;
string sqlCmd3 = “insert into user_data_comunication (user_id) values (’” + User_ID + “‘)”;
string sqlCmd4 = “insert into user_data_like (user_id) values (’” + User_ID + “‘)”;
string sqlCmd5 = “insert into user_data_schooldata (user_id) values (’” + User_ID + “‘)”;
//string sqlCmd6 = “insert into user_data_status (user_id,user_status_date) values (’” + User_ID + “‘,’” + System.DateTime.Now + “‘)”;
string[] SQLstringList = { sqlCmd1, sqlCmd2, sqlCmd3, sqlCmd4, sqlCmd5 };
for (int i = 0; i 1)
{
sqlCmd.CommandText = SQLstr;
sqlCmd.ExecuteNonQuery();
}
}
sqlCmd.Transaction.Commit();
}
catch (Exception exe)
{
sqlCmd.Transaction.Rollback();
flag = false;
//this.FinalCheck.Text = exe.Message.ToString();
}
finally
{
//close database
sqlCnn.Close();
}
September 2, 2009
How to do the multiple operations in SQL Server ??
No Comments »
No comments yet.
RSS feed for comments on this post. TrackBack URL