如何更新Datagridview Cell值?

时间:2015-09-03 17:39:24

标签: sql sql-server

我想通过单击按钮更新特定单元格值。这是我的更新功能代码,这不起作用。我想知道正确的代码

            try
                {
                    cn.Open();
                    cmd.CommandText =
                      "update  PatientDetails set PatientNumber= '" + pno.Text + "' and PatientName='" + pname.Text + "' and Age='" + age.Text + "' and Sex ='" + sex.Text + "' and PatientAddress='" + add.Text + "' and City='" + city.Text + "' and PhoneNumber='" + phone.Text + "' and EntryDate='" + edate.Text + "' and DoctorName='" + dname.Text + "' and Diagnosis='" + dig.Text + "' and  DepartmentName='" + dpname.Text + "' where PatientNumber ='" + dataGridView1.SelectedCells.ToString() + "', PatientName='" + dataGridView1.SelectedCells.ToString() + "' , Age='" + dataGridView1.SelectedCells.ToString() + "' ,  Sex ='" + dataGridView1.SelectedCells.ToString() + "' , PatientAddress='" + dataGridView1.SelectedCells.ToString() + "' , City='" + dataGridView1.SelectedCells.ToString() + "' , PhoneNumber='" + dataGridView1.SelectedCells.ToString() + "' , EntryDate='" + dataGridView1.SelectedCells.ToString() + "' . DoctorName='" + dataGridView1.SelectedCells.ToString() + "' ,Diagnosis='" + dataGridView1.SelectedCells.ToString() + "' ,  DepartmentName='" + dataGridView1.SelectedCells.ToString() + "'";
                    cmd.ExecuteNonQuery();

                    cn.Close();
                    MessageBox.Show("Record Updated Successfully");

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
        } ` 

1 个答案:

答案 0 :(得分:1)

应该是

update table_name
   set col1 = value1, col2 = value2, col3 = value3
 where col1 = value1 and col2 = value2 and col3 = value3

update table_name
   set col1 = value1 and col2 = value2 and col3 = value3
 where col1 = value1, col2 = value2, col3 = value3

请注意使用逗号(,)和and的区别。