我有以下代码我该怎么做才能减少代码

时间:2015-03-29 19:12:09

标签: 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);
    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();
}

3 个答案:

答案 0 :(得分:0)

我不知道你的整个代码,这个答案是基于你发布的内容。

    private void AddCheckBox(int num, int tag, bool isChecked)
    {
        if (isChecked)
        {
            // Maybe you can wrap the connection on a IDisposable class with this method with the open and close
            using (SqlConnection connect = new SqlConnection("Data Source=AMIN-PC;Initial Catalog=Dental_Software;Integrated Security=True"))
            {
                // You can use a prepare ?
                SqlCommand sdfaddsf = new SqlCommand("spInsertTeeth", connect);
                sdfaddsf.CommandType = CommandType.StoredProcedure;

                // Move this on the class constructor
                connect.Open();
                sdfaddsf.Parameters.AddWithValue("@InfectedTeeth", tag);

                // Really necessary ?
                MessageBox.Show(string.format("i am checkbox {0}", num));

                sdfaddsf.ExecuteNonQuery();

                // Move to the dispose method
                connect.Close();
            }
        }
    }

        AddCheckBox(8, checkBox8.Tag, checkBox8.Checked);
        AddCheckBox(9, checkBox9.Tag, checkBox9.Checked);
        AddCheckBox(10, checkBox9.Tag, checkBox9.Checked);
        [..]

答案 1 :(得分:0)

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();
}

答案 2 :(得分:0)

你应该

  • 在迭代所有复选框
  • 之前设置连接并命令一次
  • 将代码处理一个复选框放入一个方法,您可以为需要处理的每个复选框调用

这样的事情:

// 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();
}

更好的选择是获取所有已检查复选框的列表,然后只进行一次数据库调用,将这些数字作为表格传递 - 值参数到一个SQL语句,它会在单个语句中插入它们(而不是像你选中的复选框那样多次调用数据库......)