在我的项目中,我将客户端名称设置为主键,如果我输入相同的值,就会出现异常。现在我想要编写验证,即如果我重新输入主键值,则应该收到消息“数据已经存在”。请帮助我完成这个任务。我正在使用以下代码插入值:
private void btnInsert_Click(object sender, EventArgs e)
{
if (txtName.Text == string.Empty)
{
MessageBox.Show("Please enter a value to Project Name!");
txtName.Focus();
return;
}
if (txtContactPerson.Text == string.Empty)
{
MessageBox.Show("Please enter a value to Description!");
txtContactPerson.Focus();
return;
}
SqlConnection con = Helper.getconnection();
con.Open();
string commandText = "InsertClient";
SqlCommand cmd = new SqlCommand(commandText, con);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@ContactPerson", txtContactPerson.Text);
cmd.CommandType = CommandType.StoredProcedure;
MessageBox.Show("Client details are inserted successfully");
txtName.Clear();
txtContactPerson.Clear();
object Name = cmd.ExecuteNonQuery();
con.Close();
BindData();
}
InsertClient
程序没有传递任何primary key
,看起来primary key
是自动增加的,比如使用auto_increment
?否则,在插入记录时实际的primary key
是什么? - King King