当我尝试使用vwd 2010将数据插入表中时,没有显示错误但仍未插入 [英] when i try to insert data into table using vwd 2010 no error is shown but still not inserted
本文介绍了当我尝试使用vwd 2010将数据插入表中时,没有显示错误但仍未插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public partial class _1stex : System.Web.UI.Page
{
SqlConnection conn;
SqlCommand cmd;
SqlTransaction trans;
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection(ConfigurationManager.AppSettings["MasterConn"].ToString());
}
protected void Button1_Click(object sender, EventArgs e)
{
conn.Open();
trans = conn.BeginTransaction();
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.Transaction = trans;
try
{
cmd.CommandText="INSERT INTO TRANS1 VALUES("+TextBox1.Text+",'"+TextBox2.Text+"','"+TextBox3.Text+"')";
cmd.ExecuteNonQuery();
cmd.CommandText ="INSERT INTO TRANS2 VALUES(" + TextBox1.Text + ",'" + TextBox2.Text + "','" + TextBox3.Text + "')";
cmd.ExecuteNonQuery();
cmd.CommandText ="INSERT INTO TRANS3 VALUES(" + TextBox1.Text + ",'" + TextBox2.Text + "','" + TextBox3.Text + "')";
cmd.ExecuteNonQuery();
Response.Write("<script>alert('done')</script>");
}
catch(Exception ex)
{
trans.Rollback();
Response.Write("<script>alert('"+ex.Message+"')</script>");
}
finally
{
conn.Close();
TextBox1.Text = TextBox2.Text = TextBox3.Text = string.Empty;
}
}
}
推荐答案
您永远不会提交您的交易。尝试在try块的代码末尾调用事务的Commit()方法。像这样...
You never commit your transaction. Try putting a call to the transaction's Commit() method at the end of the code in your try block. Like so...
try{
// command code
// ...
// commit transaction
trans.Commit();
Response.Write("<script>alert('done')</script>");
}
catch(Exception ex){
// ...
}
finally{
// ...
}
链接到MS帮助页面上有一个例子......
http://msdn.microsoft.com/en-us/library/86773566(v=vs.110).aspx [ ^ ]
这篇关于当我尝试使用vwd 2010将数据插入表中时,没有显示错误但仍未插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文