编纂
会员
- 已加入
- 2020年12月4日
- 留言内容
- 18
- 编程经验
- 1-3
你好!我已将SQL Management Studio与创建该医院管理系统的Visual Studio相连。我已将患者ID设置为 首要的关键 在SQL Management Studio中。但是,当我输入重复的ID时,代码没有提出异议。我应该怎么做才能不允许Winform接受重复的患者ID。以下是患者登记表的代码。
C#:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A85V0ME\SQLEXPRESS;Initial Catalog=Hospitalmanagement;Integrated Security=True");
con.Open();
string gen = string.Empty;
if (radioButton1.Checked)
{
gen = "Male";
}
else
{
gen = "Female";
}
try
{
string str = "INSERT INTO patient(id,name,gen,age,date,cont,addr,disease,status,r_type,building,r_no,price) VALUES('" + textBox1.Text +"','" + textBox2.Text + "','" + gen + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox10.Text + "','" + textBox9.Text + "','" + textBox11.Text + "','" + textBox12.Text + "'); ";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
string str1 = "select max(Id) from patient;";
SqlCommand cmd1 = new SqlCommand(str1, con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
MessageBox.Show("Patient Information Saved Successfully..");
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
textBox10.Text = "";
textBox11.Text = "";
textBox12.Text = "";
}
}
catch (SqlException excep)
{
MessageBox.Show(excep.Message);
}
con.Close();
}
private void PatientRegistration_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A85V0ME\SQLEXPRESS;Initial Catalog=Hospitalmanagement;Integrated Security=True");
con.Open();
string str1 = "select max(id) from patient;";
SqlCommand cmd1 = new SqlCommand(str1, con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
string val = dr[0].ToString();
if (val == "")
{
textBox1.Text = "1";
}
else
{
int a;
a = Convert.ToInt32(dr[0].ToString());
a = a + 1;
textBox1.Text = a.ToString();
}
}
con.Close();
}