2015-02-05 110 views
0

我有一个关于C#编程的问题。如何将if语句转换为switch语句。 (textbox属性)

我正在研究如何使if语句进入循环或切换。 P.S.我之所以问这个是因为我不知道如何使这个样品符合switch语句if语句

​​

这里是完整的片段

public void btnSearch_Click(object sender, EventArgs e) 
    { 
     using (var cmdconn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString)) 
     { 

      SqlCommand cmd = new SqlCommand(); 
      cmd.Connection = cmdconn; 

      if (!string.IsNullOrEmpty(cmbDivision.Text) & 
       (string.IsNullOrEmpty(tbEnduser.Text))) 
      { 
       cmd.CommandText = @"select a.*, c.Enduser   
          from matt.ServiceInfo a 
          left join matt.Divisions b 
          on 
          a.DivisionCode = b.DivisionCode 
          left join matt.EmployeeInfo c 
          on a.DivisionCode = c.DivisionCode 
          where b.DivisionCode like @Division"; 
       cmd.Parameters.AddWithValue("@Division", cmbDivision.Text + '%'); 


        SqlDataAdapter da = new SqlDataAdapter(); 
        da.SelectCommand = cmd; 
        DataTable dt = new DataTable(); 
        btnEdit.Visible = true; 
        da.Fill(dt); 
        dgvRptView.DataSource = dt; 

      } 
      else if (!string.IsNullOrEmpty(tbEnduser.Text) & 
        (string.IsNullOrEmpty(cmbDivision.Text))) 
      { 
       cmd.CommandText = @"select a.*, c.Enduser   
          from matt.ServiceInfo a 
          left join matt.Divisions b 
          on 
          a.DivisionCode = b.DivisionCode 
          left join matt.EmployeeInfo c 
          on a.DivisionCode = c.DivisionCode 
          where c.Enduser like @Enduser"; 
       cmd.Parameters.AddWithValue("@Enduser", tbEnduser.Text + '%'); 

       SqlDataAdapter da = new SqlDataAdapter(); 
       da.SelectCommand = cmd; 
       DataTable dt = new DataTable(); 
       da.Fill(dt); 

       dgvRptView.DataSource = dt; 

      } 
      else if (!string.IsNullOrEmpty(tbEnduser.Text) & 
        (!string.IsNullOrEmpty(cmbDivision.Text))) 
      { 
       cmd.CommandText = @"select a.* 
       from matt.ServiceInfo a 
       left join matt.Divisions b 
       on a.DivisionCode = b.DivisionCode 
       where b.DivisionCode like @Division"; 
       cmd.Parameters.AddWithValue("@Enduser", tbEnduser.Text + '%'); 
       cmd.Parameters.AddWithValue("@cmbDivison", cmbDivision.Text + '%'); 
       btnEdit.Visible = true; 
       SqlDataAdapter da = new SqlDataAdapter(); 
       da.SelectCommand = cmd; 
       DataTable dt = new DataTable(); 
       da.Fill(dt); 

       dgvRptView.DataSource = dt; 

      } 
      else 
      { 
       MessageBox.Show("Please fill the following fields"); 
      } 
     } 

    } 

我的代码似乎工作正常,事情是,我想增强我的编码, 所以我决定把它转换为for循环来缩短我的代码。显然,我不熟悉每个循环的使用属性。或者如果我使用Switch case,会更好,这是我不熟悉的。有人可以帮助我在较短的行中转换我的代码吗?先谢谢你,我的兄弟姐妹。

+1

你很困惑'&'和'&&'。 –

+0

你不需要开关或者更糟的foreach循环,你需要更好地考虑如何重构你的代码来设置命令文本和你想要处理的三种情况的参数 – Steve

+0

如果你使用存储过程而不是内联查询,那么你甚至不需要使用if或switch语句。存储过程将具有如下子句作为b.DivisionCode,如%IsNull(@Division,b.DivisionCode)%和c.Enduser,如%ISNULL(@Enduser,c.Enduser)% –

回答

3

使用编译时常量值切换工作。你需要的是进去驱除掉重复的代码,如果一个表达式的各种值进行测试时,树枝

public void btnSearch_Click(object sender, EventArgs e) 
{ 
    if (string.IsNullOrEmpty(tbEnduser.Text) && string.IsNullOrEmpty(cmbDivision.Text)) 
    { 
     MessageBox.Show("Please fill the following fields"); 
     return; 
    } 
    using (var cmdconn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString)) 
    { 

     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = cmdconn; 
     string text = @"select a.*, c.Enduser   
         from matt.ServiceInfo a 
         left join matt.Divisions b 
         on 
         a.DivisionCode = b.DivisionCode 
         left join matt.EmployeeInfo c 
         on a.DivisionCode = c.DivisionCode"; 

     if (!string.IsNullOrEmpty(tbEnduser.Text) & 
       (!string.IsNullOrEmpty(cmbDivision.Text))) 
     { 
      text += " where b.DivisionCode like @Division and c.Enduser like @Enduser"; 
      cmd.Parameters.AddWithValue("@Enduser", tbEnduser.Text + '%'); 
      cmd.Parameters.AddWithValue("@cmbDivison", cmbDivision.Text + '%'); 
      btnEdit.Visible = true; 
     } 
     else if (!string.IsNullOrEmpty(cmbDivision.Text) & 
      (string.IsNullOrEmpty(tbEnduser.Text))) 
     { 
      text += " where b.DivisionCode like @Division"; 
      cmd.Parameters.AddWithValue("@Division", cmbDivision.Text + '%'); 
     } 
     else if (!string.IsNullOrEmpty(tbEnduser.Text) & 
       (string.IsNullOrEmpty(cmbDivision.Text))) 
     { 
      text+=" where c.Enduser like @Enduser"; 
      cmd.Parameters.AddWithValue("@Enduser", tbEnduser.Text + '%'); 
     } 

     cmd.CommandText = text; 
     SqlDataAdapter da = new SqlDataAdapter(); 
     da.SelectCommand = cmd; 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 

     dgvRptView.DataSource = dt; 
    } 
} 
1

你不能。

switch语句需要一个表达式,它与单个值列表(枚举,整数,字符串)进行比较。

在你的情况下,你有三个不同的表达式,没有什么可比较的。

当然,你可以欺骗你的病情为一个int:

int conditionInt = 
    string.IsNullOrEmpty(cmbDivision.Text) ? 1 : 0 
    + 
    string.IsNullOrEmpty(cmbDivision.Text) ? 2 : 0 

,然后有开关的情况下0,1,2和3 但这绝对是不推荐并认为混淆代码

你更好集中精力,否则重构你的代码:

  • 避免重复的代码,把它放在一个方法中。
  • 使用参数化的SQL


编辑

建议整体结构 而不是

if(!empty1 && !empty2) 
else if(!empty1 && empty2) 
... 

你coud构建这样的:

if(!empty1) 
{ 
    if(!empty2) 
    { 
    } 
    else // empty2 
    { 
    } 
} 
else // empty1 
{ 
    if(!empty2) 
    { 
    } 
    else // empty2 
    { 
    } 
} 

为了让更清晰和/或更具可读性,但我认为这种变化更多的是品味的问题:看它,并决定为自己,如果你4周

+0

那么这意味着最好保持这种格式?我在正确的轨道上完成我的系统? (不包括我的意大利面代码格式) –

+0

请参阅编辑我的答案 – DrKoch

1

我以后读它是什么让你更快乐不要以为开关是最好的选择。你将不得不将if语句转换为可以在switch语句中使用的某种类型,例如。枚举,数字。看起来你的SQL语句是相同的,你不能坚持2个参数查询,只是在空字符串内传递,如果是空的?

1

switch声明仅使用。这不适合你的情况,因为你需要测试多个表达式;所以if是正确的风格。

但是,我只是在if语句中执行SQL命令创建和参数绑定。在所有if语句之后放置SqlDataApator

更新

灰已经证明我说的话。请为他/她投票。

1

switch声明在这里并不是一个更好的选择。但是你可以重构你构建sql命令的方式:

var connStr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString); 
using (var cmdconn = new SqlConnection(connStr)) 
using (var cmd = new SqlCommand()) 
{ 
    cmd.Connection = cmdconn; 
    var conditions = new List<string>(); 
    if (!string.IsNullOrEmpty(cmbDivision.Text)) 
    { 
     conditions.Add("b.DivisionCode like @Division"); 
     cmd.Parameters.AddWithValue("@Division", cmbDivision.Text + '%'); 
    } 
    if (!string.IsNullOrEmpty(tbEnduser.Text)) 
    { 
     conditions.Add("c.Enduser like @Enduser"); 
     cmd.Parameters.AddWithValue("@Enduser", tbEnduser.Text + '%'); 
    } 

    var sql = @"select a.*, c.Enduser   
       from matt.ServiceInfo a 
       left join matt.Divisions b on a.DivisionCode = b.DivisionCode 
       left join matt.EmployeeInfo c on a.DivisionCode = c.DivisionCode 
       where " + string.Join(" and ", conditions); 

    cmd.CommandText = sql; 
    SqlDataAdapter da = new SqlDataAdapter(); 
    da.SelectCommand = cmd; 
    DataTable dt = new DataTable(); 
    btnEdit.Visible = true; 
    da.Fill(dt); 
    dgvRptView.DataSource = dt; 
}