无法执行更新 [英] Cannot able to perform update

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

问题描述

我填充了一个带有转发器控件的表。当用户单击编辑按钮时,它将重定向到下一页,其中可以使用修改表单。表中的数据会自动填充到这些字段中。如果用户单击更新,则更新后按钮它应该更新到数据库。但在我的情况下,点击更新后它再次回到原始数据。 s ome one plz help me.i附上我的代码



我尝试了什么:



I populated a table with repeater control.when the user click edit button it will redirect to next page where the form for modification is available.and the data from table is automatically populated into those fields.After update if the user click update button it should updated into the database.but in my case after clicking update it again turn back to the original data. some one plz help me.i attached my code

What I have tried:

public partial class StudEdit : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            String s = Request.QueryString["ID"].ToString();
            lblStudentID.Text = s;
            string constr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            con.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(
               "select * from tbl_Student where StudentID=" + s + "", con);
                SqlDataReader sd = cmd.ExecuteReader();
                while (sd.Read())
                {
                    inputName.Text = sd.GetValue(1).ToString();
                    inputClass.Text = sd.GetValue(2).ToString();
                }
            }
            catch (SqlException ex)
            {

            }
            con.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String id = Request.QueryString["ID"];
            string constr2 = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
            SqlConnection con2 = new SqlConnection(constr2);
            con2.Open();
            try
            {
                SqlCommand cmd2 = new SqlCommand("update [informationdb].[dbo].[tbl_Student] set StudentName='" + inputName.Text + "',StudentClass=" + inputClass.Text + "where StudentID=" + id + "", con2);
                cmd2.ExecuteNonQuery();
                status.Text = "Data updated sucessfully";
                cmd2.Dispose();
            }catch(Exception ex)
            {
                status.Text =""+ ex;
            }
            con2.Close();
        }
    }
}

推荐答案

一些事情...在你的页面加载上,你有一个 catch ,只是丢弃任何SQL异常。你几乎不应该那样做。通常,您应该(至少)记录或缓解异常。



在按钮单击中,将异常保存在某些状态文本中。发生异常吗?如果有的话,请你分享一下这个文字吗?



另外一些观察,你有资源泄漏。您根本不会处置 cmd 。您不会处置 sd 。而且,如果发生异常,你不会处理 cmd2



我建议你熟悉一下C#使用语句。一个典型的模式是:



A few things...on your page load, you have a catch that simply discards any SQL exception. You should almost never do that. In general, you should either (at least) log or mitigate the exception.

In your button click, you save the exception in some status text. Does an exception occur? Could you please share the text if it does?

Some additional observation, you have resource leaks. You don't dispose of cmd at all. You don't dispose of sd. And, you don't dispose of cmd2, if an exception occurs.

I suggest you familiarize yourself with the C# using statement. A typical pattern would be:

using(var con = new SqlConnection(conStr))
{
  con.Open();
  using(var cmd = new SqlCommand(cmdStr, con))
  {
    // Do work here
  }
}





使用使用语句,您不小心引入资源泄漏的可能性要小得多。



添加另一个点,在构建命令时应该使用SQL参数而不是字符串连接。否则,通过SQL注入攻击,黑客很容易成为攻击目标。



请参阅: SQL注入| Microsoft Docs [ ^ ]



最后,在没有任何异常文本的情况下,我无法明确说出您的问题可能是什么。但是,我可以说你的SQL更新命令无效。



至少,你缺少引用 StudentClass <的值/ code>列,在WHERE命令之前缺少空格。



With the using statements, there is far less chance you'll accidentally introduce a resource leak.

Adding one other point, you should use SQL parameters instead of string concatenation when building your commands. Otherwise, you are a very easy target for hackers via a SQL injection attack.

See: SQL Injection | Microsoft Docs[^]

Finally, without any exception text, I cannot say definitively what your problem might be. However, I can say that your SQL update command is not valid.

At a minimum, you are missing quoting on the value of StudentClass column and you are missing a space before the WHERE command.


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

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