ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 利用C#事务处理对数据库进行多重操作

利用C#事务处理对数据库进行多重操作

原创 Linux操作系统 作者:wangwenbincom 时间:2009-08-18 12:53:39 0 删除 编辑

C# code

SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCnn;
sqlCmd.Transaction = sqlCnn.BeginTransaction(); //完成事务处理的前期准备工作

try
{
//将用户注册的数据写入数据库
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 < SQLstringList.Length; i++)
{
string SQLstr = SQLstringList[i].ToString();
if (SQLstr.Trim().Length > 1)
{
sqlCmd.CommandText = SQLstr;
sqlCmd.ExecuteNonQuery();
}
}
sqlCmd.Transaction.Commit();
}
catch (Exception exe)
{
sqlCmd.Transaction.Rollback();
flag = false;
//this.FinalCheck.Text = exe.Message.ToString();
}
finally
{
//关闭数据库

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22328375/viewspace-612574/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-08-12

  • 博文量
    14
  • 访问量
    15271