数据库无法更新。请帮助我 [英] database can't be updated. plz help me

查看:87
本文介绍了数据库无法更新。请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

嘿,



我试试看整个声明Button2_Click并查看是否抛出了任何异常。



此外,它还有很好的编码实践来包装任何在使用语句中实现IDisposable的类,因此您的方法将成为:



Hey,

Id'd put a try catch around the whole statement in Button2_Click and see if any exceptions were thrown.

Also its good coding practice to wrap any classes which implement IDisposable in using statements so your method would become:

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.


这篇关于数据库无法更新。请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆