使用c#编辑sql数据库记录

时间:2013-01-18 00:24:57

标签: c# sql sql-server visual-studio-2010 sql-server-2012

我创建了一个连接到小型数据库的小软件。我正在使用c#和winfroms连接到本地sql服务器并在datagridview中显示数据库到目前为止,我已成功地将记录添加到数据库并选择要编辑的记录。

因此,当我选择一个记录进行编辑并编辑想要的字段时,我会点击编辑按钮,理论上应该更新编辑的记录。但是,当我这样做时,我似乎面临以下错误: (见图1)

图1 enter image description here

我似乎无法理解为什么我发生了这个错误。任何帮助将不胜感激

界面:(图2)

图2 enter image description here

执行编辑的代码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();
            }

        }

2 个答案:

答案 0 :(得分:2)

不考虑参数化问题 - 您错过了=

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

然而;我强烈建议您查看参数化。例如,如果我输入(进入该文本框)会发生什么:

0; delete from myEmployees --

答案 1 :(得分:1)

尝试在=EmpID之后添加btnEdit_Click

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