4

实际上,当我单击 datagrid 视图的行或单元格时,它们会填充到文本框中进行编辑,在我编辑并点击更新后,如果我关闭并再次运行表单,datagridview 不会立即改变,它正在改变。我的要求是单击更新按钮后它应该立即更改。我用于更新点击的代码是:

 private void btnUpdate_Click(object sender, EventArgs e)
 {
        SqlConnection con = Helper.getconnection();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        string PrjID = txtPrjID.Text;
        string PrjName = txtPrjNmae.Text;
        string Description = txtPrjdescription.Text;
        string Date = txtPrjDate.Text;
        string Size = txtPrjSize.Text;
        string Manager = txtPrjManager.Text;
        cmd.CommandText = "Update Projects set ProjectName= '" + PrjName + "', Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectID= " + PrjID + " ";
        MessageBox.Show("Project Details are updated");
        dataGridView2.Update();
        dataGridView2.Refresh();
        con.Open(); 
        cmd.ExecuteNonQuery();
        con.Close();
  }

请告诉我我在做什么错误。

4

3 回答 3

3

要记住两件重要的事情:

首先,在使用 DB 或 Streams 时,您应该使用using语句或 try catch finally 并关闭finally块中的连接。

其次,如果您想更新dataGridView数据库更新后,那么您应该更新后进行。在您的代码中,您在更新之前正在执行此操作。

第三,也是最重要的,您的 DB 命令很危险,并且对SQL 注入开放。使用参数化命令几乎总是更好:

private void btnUpdate_Click(object sender, EventArgs e)
{
    UpdateDB();
    dataGridView2.Update();
    dataGridView2.Refresh();
}

private void UpdateDB()
{
    using (DbConnection con = Helper.getconnection())
    {
        con.Open();

        using(DbCommand cmd = con.CreateCommand("Update Projects set ProjectName= @PrjName, Description=@Description, DateStarted=@Date, TeamSize=@Size,Manager=@Manager where ProjectID=@PrjID "))
        {

           cmd.CommandType = CommandType.Text;
           cmd.Parameters.Add(new SqlParameter("PrjName", txtPrjNmae.Text));
           cmd.Parameters.Add(new SqlParameter("Description", txtPrjdescription.Text));
           cmd.Parameters.Add(new SqlParameter("Date", txtPrjDate.Text));
           cmd.Parameters.Add(new SqlParameter("Size", txtPrjSize.Text));
           cmd.Parameters.Add(new SqlParameter("Manager", txtPrjManager.Text));
           cmd.Parameters.Add(new SqlParameter("PrjID", txtPrjID.Text));
           cmd.Connection = con;

           cmd.ExecuteNonQuery();
        }
    }
}

现在我不知道您是如何将数据绑定到的dataGridView,但可能需要再次从数据库中获取数据。然而,这部分只包含调用您在程序开始时执行的相同命令

于 2013-09-09T11:47:13.773 回答
2

好吧,我会说您以错误的顺序开始调用事物...并且您确实应该在 using 语句中建立连接。

protected void btnUpdate_Click(object sender, EventArgs e)
{
    // Update DB first
    UpdateProjectDetails(txtPrjID.Text, txtPrjNmae.Text, txtPrjdescription.Text, txtPrjDate.Text, txtPrjSize.Text, txtPrjManager.Text);

    // Fetch new results from DB
    IEnumerable<ProjectDetail> projectDetails = GetProjectDetails();

    // Update UI
    dataGridView2.DataSource = projectDetails;
    dataGridView2.Update();
    dataGridView2.Refresh();

    // Alert the user
    MessageBox.Show("Project Details are updated");
}

public void UpdateProjectDetails(string prjID, string prjName string description, string date, string size, string manager)
{
    using (DbConnection con = Helper.getconnection())
    {
        con.Open();

        DbCommand cmd = con.CreateCommand();

        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Update Projects set ProjectName= '" + PrjName + "', Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectID= " + PrjID + " ";
        cmd.Connection = con;

        cmd.ExecuteNonQuery();
    }
}
于 2013-09-09T10:00:47.293 回答
1
SqlConnection con = Helper.getconnection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            cmd.CommandType = CommandType.Text;
            string PrjName = txtPrjNmae.Text;
            string Description = txtPrjdescription.Text;
           string Date = txtPrjDate.Text;
            string Size = txtPrjSize.Text;
            string Manager = txtPrjManager.Text;
            cmd.CommandText = "Update Projects set Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectName= '" + PrjName + "' ";
            MessageBox.Show("Project Details are updated");
            dataGridView2.Update();
            dataGridView2.Refresh();
            cmd.ExecuteNonQuery();
            con.Close();
于 2013-09-26T08:29:17.743 回答