簡體   English   中英

使用C#編輯SQL數據庫記錄

[英]Editing an sql database record using c#

我創建了一個連接到小型數據庫的小型軟件。 到目前為止,我正在使用c#和winfroms連接到本地sql服務器並在datagridview顯示數據庫,我已經成功地將記錄成功添加到數據庫並選擇了要編輯的記錄。

因此,當我選擇要編輯的記錄並編輯所需字段時,我單擊了“編輯”按鈕,從理論上講,該按鈕應更新已編輯的記錄。 但是,當我這樣做時,似乎會遇到以下錯誤:(見圖1)

圖。1 在此處輸入圖片說明

我似乎無法弄清楚為什么我會發生此錯誤。 任何幫助將非常感激

接口:(圖2)

圖2 在此處輸入圖片說明

進行編輯btnEdit的代碼:

  private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                //Open Connection
                sc.Open();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM myEmployees WHERE EmpID" + 
                    txtID.Text + " ", sc);
                da.Fill(dt);

                //start the editing of the selected record
                dt.Rows[0].BeginEdit();

                dt.Rows[0][1] = txtFName.Text;
                dt.Rows[0][2] = txtLName.Text;
                dt.Rows[0][3] = txtJRole.Text;
                dt.Rows[0][4] = txtEmp.Text;
                //dt.Rows[0][1] =

                //stop editing
                dt.Rows[0].EndEdit();

                //sql commandbuilder that allow saving of records
                SqlCommandBuilder cb = new SqlCommandBuilder(da);

                //update the database
                da.Update(dt);

                //close connection
                sc.Close();

                loadEmp();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Application.ExitThread();
            }
        }

datagridview cick事件,它負責選擇記錄進行編輯:

private void dgEmployees_Click(object sender, EventArgs e)
        {
            try
            {


                DataTable dt = new DataTable();
                SqlDataAdapter slctRow = new SqlDataAdapter("SELECT * FROM myEmployees WHERE EmpID=" +
                Convert.ToInt16(dgEmployees.SelectedRows[0].Cells[0].Value.ToString()) + " ", sc);
                slctRow.Fill(dt);

                //display records into textboxes
                txtID.Text = dt.Rows[0][0].ToString();
                txtFName.Text = dt.Rows[0][1].ToString();
                txtLName.Text = dt.Rows[0][2].ToString();
                txtJRole.Text = dt.Rows[0][3].ToString();
                txtEmp.Text = dt.Rows[0][4].ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                // Application.ExitThread();
            }

        }

拋開參數化的問題-您錯過了=

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM myEmployees WHERE EmpID="+ 
                txtID.Text + " ", sc); // WARNING: SQL INJECTION RISK

然而; 強烈建議您看一下參數化。 例如,如果我鍵入(在該文本框中)會發生什么:

0; delete from myEmployees --

嘗試在btnEdit_Click EmpID之后添加=

 SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM myEmployees WHERE EmpID=" + 
                    txtID.Text + " ", sc);

暫無
暫無

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

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