我正在尝试更新记录并引发异常。这段代码有错误吗?
try
{
using (SqlConnection conn = new SqlConnection(Common.GetDBConnectionString()))
{
conn.Open();
using (SqlCommand cmd =
new SqlCommand("UPDATE dbo.Tb_Patient SET Name = @PatientName, Age = @PatientAge, Contact = @PatientContact, Date = @PatientDate, Occupation = @PatientOccupation, Gender = @PatientGender " +
" WHERE Id=@SetVForText", conn))
{
cmd.Parameters.AddWithValue("@Id", SetVForText);
cmd.Parameters.AddWithValue("@Name", txtname.Text);
cmd.Parameters.AddWithValue("@Age", txtage.Text);
cmd.Parameters.AddWithValue("@Contact", txtcontact.Text);
cmd.Parameters.AddWithValue("@Date", txtdate.Text);
cmd.Parameters.AddWithValue("@Occupation", txtoccupation.Text);
cmd.Parameters.AddWithValue("@Gender",comboBox1.SelectedItem.ToString() );
int rows = cmd.ExecuteNonQuery();
MessageBox.Show("Update successfully");
//rows number of record got updated
}
}
}
catch (SqlException ex)
{
MessageBox.Show("Updation failed"+ex);
}