它是C#SQL Transaction的正确代码吗? [英] IS it right code for C# SQL Transaction ?
本文介绍了它是C#SQL Transaction的正确代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的专家,
Pl查看我的代码。是否怀疑Sql& C#Transcation代码? Pl提供更多微调代码的建议。
Dear Experts,
Pl see my code. Is it wright Sql & C# Transcation code? Pl give the advice for more fine tune code.
private void btnsave_Click(object sender, EventArgs e)
{
string sq = null;
try
{
sq = "Insert into unpw (username,password)values(@username,@password)";
cmd = new System.Data.SqlClient.SqlCommand(sq, con);
Trans = con.BeginTransaction();
cmd.Parameters.AddWithValue("@username", txtun.Text);
cmd.Parameters.AddWithValue("@password", txtpwd.Text);
cmd.Transaction = Trans;
cmd.ExecuteNonQuery();
Trans.Commit();
sq = "Insert into unpw1 (username,password)values(@username,@password)";
cmd = new System.Data.SqlClient.SqlCommand(sq, con);
cmd.Parameters.AddWithValue("@UserName", txtun.Text);
cmd.Parameters.AddWithValue("@password", txtpwd.Text);
cmd.Transaction = Trans;
cmd.ExecuteNonQuery();
//Trans.Commit();
MessageBox.Show("Save");
cmd.Dispose();
con.Close();
}
catch (Exception ex)
{
Trans.Rollback();
MessageBox.Show(ex.Message);
}
}
谢谢&提前
sreeni。
Thanks & Advance
sreeni.
推荐答案
使用以下更改更改您的代码。我假设您已在代码中的某处声明了Trans变量
Change your code with the below changes. I assume that you have declare Trans variable somewhere in your code
private void btnsave_Click(object sender, EventArgs e)
{
string sq = null;
try
{
Trans = con.BeginTransaction();
sq = "Insert into unpw (username,password)values(@username,@password)";
cmd = new System.Data.SqlClient.SqlCommand(sq, con,Trans);
cmd.Parameters.AddWithValue("@username", txtun.Text);
cmd.Parameters.AddWithValue("@password", txtpwd.Text);
cmd.Transaction = Trans;
cmd.ExecuteNonQuery();
sq = "Insert into unpw1 (username,password)values(@username,@password)";
cmd = new System.Data.SqlClient.SqlCommand(sq, con,Trans);
cmd.Parameters.AddWithValue("@UserName", txtun.Text);
cmd.Parameters.AddWithValue("@password", txtpwd.Text);
cmd.Transaction = Trans;
cmd.ExecuteNonQuery();
Trans.Commit();
MessageBox.Show("Save");
cmd.Dispose();
con.Close();
}
catch (Exception ex)
{
Trans.Rollback();
MessageBox.Show(ex.Message);
}
}
protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString);
con.Open();
SqlTransaction Trans = con.BeginTransaction();
string sq = null;
try
{
sq = "Insert into unpw (username,password)values(@username,@password)";
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sq, con, Trans);
cmd.Parameters.AddWithValue("@username", txtun.Text);
cmd.Parameters.AddWithValue("@password", txtpwd.Text);
cmd.Transaction = Trans;
int i= cmd.ExecuteNonQuery();
sq = "Insert into unpw1 (username,password)values(@username,@password)";
//cmd = new System.Data.SqlClient.SqlCommand(sq, con, Trans);
//cmd.Parameters.AddWithValue("@UserName", txtun.Text);
//cmd.Parameters.AddWithValue("@password", txtpwd.Text);
//cmd.Transaction = Trans;
cmd.CommandText = sq;
cmd.ExecuteNonQuery();
Trans.Commit();
MessageBox.Show("Save");
cmd.Dispose();
con.Close();
}
catch (Exception ex)
{
Trans.Rollback();
MessageBox.Show(ex.Message);
}
}
无需再写下一行(如果声明是相同的):
No need to write once again the following line (if the statement is same):
//cmd = new System.Data.SqlClient.SqlCommand(sq, con, Trans);
//cmd.Parameters.AddWithValue("@UserName", txtun.Text);
//cmd.Parameters.AddWithValue("@password", txtpwd.Text);
//cmd.Transaction = Trans;
这篇关于它是C#SQL Transaction的正确代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文