简体   繁体   English

SQL Server数据库未更新C#

[英]SQL Server database is not updating C#

I'm new to database programming and C#. 我是数据库编程和C#的新手。 I'm using SQL server database and connected it to my winforms application. 我正在使用SQL服务器数据库并将其连接到我的winforms应用程序。 Everything is fine, i can add new rows, and read information from the database but when i try to edit values, it does not seem to work. 一切都很好,我可以添加新行,并从数据库中读取信息但是当我尝试编辑值时,它似乎不起作用。

Here is the code i'm using. 这是我正在使用的代码。

         private void btneUpdate_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"
                Data Source = localhost; 
                Initial Catalog = BookStore; 
                Integrated Security = True;");
            SqlCommand cmd;               

            if(MessageBox.Show("You are about to save the changes. You won't be able to undo those changes.", "Update fields", MessageBoxButtons.OKCancel) == DialogResult.Yes)
            {                                      
                con.Open();
                cmd = new SqlCommand(@"UPDATE Book 
                                        SET   BookTitle = '"+ txteTitle.Text
                                        +"', BookAuthorLname = '"+txteAuthorLname.Text
                                        +"', BookAuthorFname = '"+txteAuthorFname.Text
                                        +"', BookPrice = '"+ Convert.ToDecimal(eprice)
                                        +"', BookDescription = '"+txteDesc.Text
                                        +"', DatePublication = '"+dtpePublished.Value.Date
                                        +"', BookStock = '"+ Convert.ToInt32(estock)
                                        +"', isFiction = '"+ checkboxbool
                                        +"', BookCategory = '"+ cmbeCategory.SelectedValue
                                        +"'  WHERE ISBN = '"+ txteISBN.Text +"';", con);
                cmd.ExecuteNonQuery();
                con.Close();                   
            }

            BindEdit();
            BindGrid();
        }

This part of your line is wrong 你的这一部分是错误的

..... MessageBoxButtons.OKCancel) == DialogResult.Yes)

you should check for DialogResult.OK otherwise you will never enter the update code 你应该检查DialogResult.OK否则你永远不会输入更新代码

..... MessageBoxButtons.OKCancel) == DialogResult.OK)

Said that, please stop a moment and take a bit of your time learning how to create parameterized queries . 说,请停下来,花点时间学习如何创建参数化查询 These are the only correct way to write code that interacts with a database. 这些是编写与数据库交互的代码的唯一正确方法。 String concatenation is really a bad practice and leads to Sql Injection attacks 字符串连接实际上是一种不好的做法,并导致Sql Injection攻击

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

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