本文介绍了数据库无法更新。请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("server=DIPESH-PC\\SQLEXPRESS;uid=sa;pwd=rerf;database=Employee");
SqlConnection con1 = new SqlConnection("server=DIPESH-PC\\SQLEXPRESS;uid=sa;pwd=rerf;database=Employee");
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from tbl_emp", con1);
SqlDataReader reader;
con1.Open();
reader = cmd.ExecuteReader();
reader.Read();
TextBox1.Text = reader["email"].ToString();
TextBox2.Text = reader["password"].ToString();
TextBox3.Text = reader["Address"].ToString();
reader.Close();
con1.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
this.TextBox1.ReadOnly = true;
this.TextBox2.ReadOnly = false;
this.TextBox3.ReadOnly = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd1= new SqlCommand("UPDATE tbl_emp SET password=@password, Address=@Address WHERE email=@email",conn);
cmd1.Parameters.AddWithValue("@password",TextBox2.Text);
cmd1.Parameters.AddWithValue("@Address", TextBox3.Text);
cmd1.Parameters.AddWithValue("@email", TextBox1.Text);
cmd1.ExecuteNonQuery();
conn.Close();
Label1.Text = " Data Updates Succesfully on " + System.DateTime.Now.ToShortDateString();
}
}
显示Label1消息'数据更新成功。 ...............'但数据库无法更新。 PLZ帮帮我
Label1 message displayed ' Data Updates Succesfully on................' but database can't be updated. plz help me
推荐答案
using (conn)
{
conn.Open();
using (SqlTransaction objTrans = conn.BeginTransaction())
{
try
{
using (SqlCommand cmd1 = new SqlCommand("UPDATE tbl_emp SET password=@password, Address=@Address WHERE email=@email", conn))
{
cmd1.Parameters.AddWithValue("@password", "");
cmd1.Parameters.AddWithValue("@Address", "");
cmd1.Parameters.AddWithValue("@email", "");
cmd1.ExecuteNonQuery();
objTrans.Commit();
}
}
catch (SqlException ex)
{
//Display error here
objTrans.Rollback();
}
Label1.Text " Data Updates Succesfully on " + System.DateTime.Now.ToShortDateString();
}
}
另外,为了安全起见,我会将更新包装在一个事务中,如果有一些语法错误而道歉,因为我是自由写的,但你明白了。
Also I would wrap the update in a transaction just to be safe, apologies if there are some syntax mistakes as I wrote it free hand but you get the idea.
这篇关于数据库无法更新。请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!