简体   繁体   English

c# - 如何为datagridview实现更新按钮并使用dataHandler类(数据访问类)将值保存到数据库

[英]how to implement update button for datagridview and save values to database using a dataHandler class(data access class) c#

good day, hope everyone is well :)美好的一天,希望大家都好:)

I'm a first year student, and we just started with windows forms.我是一年级学生,我们刚开始接触窗体。 I want to update data from DataGridview into my database using a button but I have to create an update method in a data handler class and call it in my form load, this is what I have tried so far.我想使用按钮将DataGridview数据更新到我的数据库中,但我必须在数据处理程序class创建一个更新方法并在我的表单加载中调用它,这是我迄今为止尝试过的。

DataHandler class:数据处理器类:

        public void UpdateStudent(int number, string name, string surname, string dob, string gender, int phone, string address)
    {
        try
        {
            //string updateQuery = @"UPDATE Student SET number='" + number + "'name='" + name + "'surname='" + surname + "'dob='" + dob + "'gender'" + gender + "'phone='" + phone + "'address='" + address + "'";
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.ExecuteNonQuery();
            MessageBox.Show("updated successfully....");
        }
        catch (Exception ex)
        {
            MessageBox.Show("not updated!", ex.Message); m
        }
        finally
        {
            conn.Close();
        }
    }

update button on form load:表单加载时的更新按钮:

  DataHandler dh = new DataHandler();

 dh.UpdateStudent(int.Parse(txtStuID.Text), txtStuName.Text, txtStuSurname.Text,txtStuDOB.Text,txtStuGender.Text, int.Parse(txtStuNo.Text), txtStuAddress.Text);
       

This is how you need to initialize SqlCommand :这是你需要如何初始化SqlCommand

new SqlCommand(queryString, connection);

You have missed passing any parameters.您错过了传递任何参数。 You will need to pass updateQuery as queryString and you will also need a proper connection string.您需要将updateQuery作为queryString传递,并且您还需要一个正确的连接字符串。

I think the actual issues that brought you here are a couple of key issues with your SQL being invalid.我认为将您带到这里的实际问题是您的 SQL 无效的几个关键问题。 However, with that having been said, the comments regarding SQL injection are absolutely correct, and you would do well to learn about parameterized SQL.然而,话虽如此,关于SQL注入的评论是绝对正确的,你最好学习参数化SQL。

First thing that jumps out is that your parameters need to be comma separated for this to be valid SQL.跳出来的第一件事是您的参数需要以逗号分隔才能成为有效的 SQL。 The second thing is that you need to be identifying the row you wish to update with a WHERE clause, your current SQL statement is going to update every record in your table, not a single record.第二件事是,您需要使用 WHERE 子句标识要更新的行,您当前的 SQL 语句将更新表中的每条记录,而不是单个记录。 Basically, your "number" field should not be in the list of columns to be updated, it needs to be moved to your WHERE clause.基本上,您的“数字”字段不应该在要更新的列列表中,它需要移动到您的 WHERE 子句中。

Below is not a full solution to your homework assignment, but a snippet of the SQL with the issues corrected.以下不是您家庭作业的完整解决方案,而是已更正问题的 SQL 片段。 One thing you can do to help yourself as you learn is to try to get your SQL to compile and run outside of your program, and then implement it in your program once you have a working statement.在学习过程中,您可以做的一件事是尝试让您的 SQL您的程序之外编译和运行,然后在您有了工作语句后在您的程序中实现它。

UPDATE Student SET name = 'John', surname = 'Doe' WHERE number = 1 UPDATE Student SET name = 'John', surname = 'Doe' WHERE number = 1

thank you for your help and patience, I just found a similar code on this site, now I'm ready for my test on Monday.谢谢你的帮助和耐心,我刚刚在这个网站上找到了一个类似的代码,现在我准备好在周一进行测试了。 :) :)

if anyone is also struggling with this too, here is the link.如果有人也在为此而苦苦挣扎,这里是链接。

crud-operations-in-windows-application-using-c-sharp-part-2 crud-operations-in-windows-application-using-c-sharp-part-2

hope you enjoy your day or night.希望你喜欢你的白天或黑夜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM