【问题标题】:How to use inline c# if instead of using long c# if statement [duplicate]如何使用内联 c# if 而不是使用长 c# if 语句 [重复]
【发布时间】:2017-12-18 10:43:38
【问题描述】:

如何使用单行 if 语句来减少此类编码的代码行数

        if (string.IsNullOrEmpty(txtpictext.Text))
        {
            Cmd.Parameters.AddWithValue("@pictext", DBNull.Value);
        }
        else
        {
            Cmd.Parameters.AddWithValue("@pictext, txtpictext.Text);
        }

        Conn.Open();
        Cmd.ExecuteNonQuery();

【问题讨论】:

标签: c# if-statement


【解决方案1】:

你想使用三元运算符 ?:

Cmd.Parameters.AddWithValue("@pictext, string.IsNullOrEmpty(txtpictext.Text) 
  ? DBNull.Value 
  : txtpictext.Text);

Conn.Open();
Cmd.ExecuteNonQuery();

像这样。

【讨论】:

    【解决方案2】:
    string assignedValue = string.Empty;
    assignedValue = string.IsNullOrEmpty(txtpictext.Text) ? DBNull.Value : txtpictext.Text ;
    Cmd.Parameters.AddWithValue("@pictext", assignedValue);
    Conn.Open();
    Cmd.ExecuteNonQuery();
    

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 2016-12-08
      • 1970-01-01
      相关资源
      最近更新 更多