簡體   English   中英

SQL Server數據庫未更新C#

[英]SQL Server database is not updating C#

我是數據庫編程和C#的新手。 我正在使用SQL服務器數據庫並將其連接到我的winforms應用程序。 一切都很好,我可以添加新行,並從數據庫中讀取信息但是當我嘗試編輯值時,它似乎不起作用。

這是我正在使用的代碼。

         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();
        }

你的這一部分是錯誤的

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

你應該檢查DialogResult.OK否則你永遠不會輸入更新代碼

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

說,請停下來,花點時間學習如何創建參數化查詢 這些是編寫與數據庫交互的代碼的唯一正確方法。 字符串連接實際上是一種不好的做法,並導致Sql Injection攻擊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM