如何更新SQL数据库中的现有resord [英] How to update an existing resord in a SQL database

查看:88
本文介绍了如何更新SQL数据库中的现有resord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我有一个包含5个文本框的网络表单。可以填写文本框并将其保存到数据库中。我有一个现有的表,其中包含已包含数据的列。我在表格中添加了4个新列。我试图将用户刚刚输入5个文本框的新数据保存到现有记录中,而不会更改或保存两次相同的记录。有没有办法做到这一点?以下是我到目前为止的情况。



Hello all.
I have a web form that has 5 text boxes in it. The text boxes can be filled out and saved to the database. I have an existing table that has columns with data already in them. I added 4 new columns to the table. I am trying to save the new data that the user just entered into the 5 text boxes to the existing record without changing or saving the same record twice. Is there a way to do that? Here is what I have so far.

protected void ButtonSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("Update Table22 (INST_ID, TOTAL_REVE, INSTRUCTIO, RESEARCH, PUBLIC_SER) values (@INST_ID, @TOTAL_REVE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER)", con);
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
            cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxFY09_2.Text);
            cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxFY09_3.Text);
            cmd.Parameters.AddWithValue("@RESEARCH", TextBoxFY09_4.Text);
            cmd.Parameters.AddWithValue("@PUBLIC_SER", TextBoxFY09_5.Text);
            
            

            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}





到目前为止这是对的吗?我是在正确的轨道还是离开?



Is this right so far? Am I on the right track or am I way off?

推荐答案

不,不是。您编写了一个UPDATE查询,就像您对INSERT查询所做的那样。但这不会像这样工作。



最好的是拥有要更新的记录的主键。然后UPDATE查询应如下所示:

No, it's not. You wrote an UPDATE query like you would have done for an INSERT query. But that does not work like this.

The best is to have the primary key of the record you want to update. Then the UPDATE query should look like:
"UPDATE Table22 SET TOTAL_REVE = @TOTAL_REVE, INSTRUCTIO = @INSTRUCTIO, RESEARCH = @RESEARCH, PUBLIC_SER = @PUBLIC_SER WHERE INST_ID = @INST_ID"





希望这会有所帮助。祝你好运。



Hope this helps. Good luck.


关于 phil.o [ ^ ]回答,我建议你使用存储过程 [ ^ ]而不是后面的代码查询,例如:



With relation to phil.o[^] answer, i would recommend you to use stored procedures[^] instead the query in code behind, for example:

CREATE PROCEDURE UpdateSomething
    @InputParameter1 DataType,
    @InputParameter1 DataType,
    ...
    @InputParameterN DataType,
AS
BEGIN
    UPDATE TableName
    SET Field1 = @InputParameter1,
    Field2 = @InputParameter2,
    ....
    FieldN = @InputParameterN

END





如需了解更多信息,请参阅:如何:执行返回行的存储过程 [ ^ ]并按照窗口左侧的相关链接进行操作。



应保护ASP.NET站点免受 SQL注入 [ ^ ]。

如何:防止ASP.NET中的SQL注入 [ ^ ]

停止SQL注射攻击在他们阻止你之前 [ ^ ]

SQL注入及其如何避免 [ ^ ]



For further information, please see: How to: Execute a Stored Procedure that Returns Rows[^] and follow the related links on the left side of window.

ASP.NET sites should be protected from SQL Injection[^].
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Injection and how to avoid it[^]


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

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