if (DropDownList1.SelectedItem.Value == "0")
        {            string connectionString = ConfigurationManager.ConnectionStrings["callbackConnectionString"].ConnectionString;
            SqlConnection MyConn = new SqlConnection(connectionString);
            MyConn.Open();
            SqlCommand MyCommand4 = new SqlCommand("update T_Account set callbackbs=" + DropDownList1.SelectedItem.Value + ",callbackyincang0='" + DropDownList2.SelectedItem.Value + "',smsyincang1='" + DropDownList3.SelectedItem.Value + "' where AcctId = '" + Session["CallbackLoginId"].ToString() + "'", MyConn);
            MyCommand4.CommandTimeout = 8000;
            MyCommand4.CommandType = CommandType.Text;
            MyCommand4.ExecuteNonQuery();
            MyConn.Close();
        }
        if (DropDownList1.SelectedItem.Value == "1")
        {            string connectionString = ConfigurationManager.ConnectionStrings["callbackConnectionString"].ConnectionString;
            SqlConnection MyConn = new SqlConnection(connectionString);
            MyConn.Open();
            SqlCommand MyCommand4 = new SqlCommand("update T_Account set callbackbs=" + DropDownList1.SelectedItem.Value + ",callbacktouchuan0='" + DropDownList2.SelectedItem.Value + "',smstouchuan1='" + DropDownList3.SelectedItem.Value + "',huilinghaoma='" + TextBox1.Text + "' where AcctId = '" + Session["CallbackLoginId"].ToString() + "'", MyConn);
            MyCommand4.CommandTimeout = 8000;
            MyCommand4.CommandType = CommandType.Text;
            MyCommand4.ExecuteNonQuery();
            MyConn.Close();
        }        if (DropDownList6.SelectedItem.Value == "0")
        {            string connectionString = ConfigurationManager.ConnectionStrings["callbackConnectionString"].ConnectionString;
            SqlConnection MyConn = new SqlConnection(connectionString);
            MyConn.Open();
            SqlCommand MyCommand4 = new SqlCommand("update T_Account set callbackbs1=" + DropDownList6.SelectedItem.Value + ",callbackyincang1='" + DropDownList4.SelectedItem.Value + "',smsyincang='" + DropDownList5.SelectedItem.Value + "' where AcctId = '" + Session["CallbackLoginId"].ToString() + "'", MyConn);
            MyCommand4.CommandTimeout = 8000;
            MyCommand4.CommandType = CommandType.Text;
            MyCommand4.ExecuteNonQuery();
            MyConn.Close();
        }
        if (DropDownList6.SelectedItem.Value == "1")
        {            string connectionString = ConfigurationManager.ConnectionStrings["callbackConnectionString"].ConnectionString;
            SqlConnection MyConn = new SqlConnection(connectionString);
            MyConn.Open();
            SqlCommand MyCommand4 = new SqlCommand("update T_Account set callbackbs1=" + DropDownList6.SelectedItem.Value + ",callbacktouchuan1='" + DropDownList4.SelectedItem.Value + "',smstouchuan='" + DropDownList5.SelectedItem.Value + "',bochuhaoma='" + TextBox2.Text + "' where AcctId = '" + Session["CallbackLoginId"].ToString() + "'", MyConn);
            MyCommand4.CommandTimeout = 8000;
            MyCommand4.CommandType = CommandType.Text;
            MyCommand4.ExecuteNonQuery();
            MyConn.Close();
        }

解决方案 »

  1.   

            public void abc()
            {
                if (DropDownList1.SelectedItem.Value == "0")
                {
                    OpData("update T_Account set callbackbs=" + DropDownList1.SelectedItem.Value + ",callbackyincang0='" + DropDownList2.SelectedItem.Value + "',smsyincang1='" + DropDownList3.SelectedItem.Value + "' where AcctId = '" + Session["CallbackLoginId"].ToString() + "'");
                }
                if (DropDownList1.SelectedItem.Value == "1")
                {
                    OpData("update T_Account set callbackbs=" + DropDownList1.SelectedItem.Value + ",callbacktouchuan0='" + DropDownList2.SelectedItem.Value + "',smstouchuan1='" + DropDownList3.SelectedItem.Value + "',huilinghaoma='" + TextBox1.Text + "' where AcctId = '" + Session["CallbackLoginId"].ToString() + "'");
                }            if (DropDownList6.SelectedItem.Value == "0")
                {
                    OpData("update T_Account set callbackbs1=" + DropDownList6.SelectedItem.Value + ",callbackyincang1='" + DropDownList4.SelectedItem.Value + "',smsyincang='" + DropDownList5.SelectedItem.Value + "' where AcctId = '" + Session["CallbackLoginId"].ToString() + "'");            }
                if (DropDownList6.SelectedItem.Value == "1")
                {
                    OpData("update T_Account set callbackbs1=" + DropDownList6.SelectedItem.Value + ",callbacktouchuan1='" + DropDownList4.SelectedItem.Value + "',smstouchuan='" + DropDownList5.SelectedItem.Value + "',bochuhaoma='" + TextBox2.Text + "' where AcctId = '" + Session["CallbackLoginId"].ToString() + "'");            }
            }        private void OpData(string sqlstr)
            {
                string connectionString = ConfigurationManager.ConnectionStrings["callbackConnectionString"].ConnectionString;
                SqlConnection MyConn = new SqlConnection(connectionString);
                MyConn.Open();
                SqlCommand MyCommand4 = new SqlCommand(sqlstr, MyConn);
                MyCommand4.CommandTimeout = 8000;
                MyCommand4.CommandType = CommandType.Text;
                MyCommand4.ExecuteNonQuery();
                MyConn.Close();
            }大体上就是个这
      

  2.   

    把这个语句用proc来写,用sqlhelper,更简单,一行代码解决了,还用了参数缓存,更有效率
      

  3.   

    用SWITCH不会更好吗?
    再把执行语句独立出来。