Sql server 我有以下代码我能做些什么来减少代码
我不知道你的全部代码,这个答案是基于你发布的内容Sql server 我有以下代码我能做些什么来减少代码,sql-server,Sql Server,我不知道你的全部代码,这个答案是基于你发布的内容 if (checkBox8.Checked) { SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True"); SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect); s
if (checkBox8.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox8.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox8");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox7.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox7.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox7");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox6.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox6.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox6");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox5.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox5.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox5");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox1.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox1.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox1");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox15.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox15.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox15");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox2.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox2.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox2");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox3.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox3.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox3");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox4.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox4.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox4");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox9.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox9.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox9");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox16.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox16.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox16");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox14.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox14.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox14");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox10.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox10.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox10");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox11.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox11.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox11");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox12.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox12.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox12");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox13.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox13.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox13");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox18.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox18.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox18");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox19.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox19.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox19");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox20.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox20.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox20");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox21.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox21.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox21");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox17.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox17.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox17");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox22.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox22.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox22");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox23.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox23.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox23");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox24.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox24.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox24");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox25.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox25.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox25");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox26.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox26.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox26");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox32.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox32.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox32");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox31.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox31.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox31");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox30.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox30.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox30");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox29.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox29.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox29");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox28.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox28.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox28");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
if (checkBox27.Checked) {
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
int checkbox = Convert.ToInt32(checkBox27.Tag);
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", checkbox);
MessageBox.Show("i am checkbox27");
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
你应该
- 在遍历所有复选框之前,设置一次连接和命令
- 将处理一个复选框的代码放入一个方法中,您可以为需要处理的每个复选框调用该方法
if (checkBox1.Checked)
doQuery(Convert.ToInt32(checkBox1.Tag))
if (checkBox2.Checked)
doQuery(Convert.ToInt32(checkBox2.Tag))
if (checkBox3.Checked)
doQuery(Convert.ToInt32(checkBox3.Tag))
...
if (checkBox99.Checked)
doQuery(Convert.ToInt32(checkBox99.Tag))
public void doQuery(int v)
{
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", v);
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
更好的选择是获取所有选中复选框的列表,然后只进行一次数据库调用,将这些数字作为表值参数传递给一个SQL语句,该语句将在单个语句中插入这些复选框(不要像选中复选框一样多次调用数据库…请任何人都能帮助我,实际上每个牙齿都有32个复选框,这是哪种语言?你能编辑你的帖子并添加相关标签吗
if (checkBox1.Checked)
doQuery(Convert.ToInt32(checkBox1.Tag))
if (checkBox2.Checked)
doQuery(Convert.ToInt32(checkBox2.Tag))
if (checkBox3.Checked)
doQuery(Convert.ToInt32(checkBox3.Tag))
...
if (checkBox99.Checked)
doQuery(Convert.ToInt32(checkBox99.Tag))
public void doQuery(int v)
{
SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True");
SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
sdfaddsf.CommandType = CommandType.StoredProcedure;
connect.Open();
sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", v);
sdfaddsf.ExecuteNonQuery();
connect.Close();
}
// set up the connection and command **ONCE** before you start iterating
// Best Practice is to put those into "using" blocks
using (SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True"))
using (SqlCommand cmdInsert = new SqlCommand("dbo.spInsertTeeth", connect))
{
cmdInsert.CommandType.StoredProcedure;
cmdInsert.Parameters.Add("@InfectedTeeth", SqlDbType.Int);
connect.Open();
// now iterate over your checkboxes - this could also be
HandleCheckbox(checkBox8);
HandleCheckbox(checkBox7);
// etc., until you're done
connect.Close();
}
public void HandleCheckbox(Checkbox checkbox)
{
int chkValue = Convert.ToInt32(checknox.Tag);
cmdInsert.Parameters["@InfectedTeeth"].Value = chkValue;
MessageBox.Show("I am " + checkbox.Name);
cmdInsert.ExecuteNonQuery();
}