2017-12-18 238 views
0

如何,如果减少的代码行数,这些类型的编码如何使用内联C#的,如果不是使用长C#的if语句

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

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

所以你知道有一个*“内联c#如果”*,但它很难使用或什么? – Sinatr

+0

重复https://stackoverflow.com/q/1678311/993547,还有很多更多。 –

回答

5

的您要使用的三元操作语句中使用单行?:

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

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

像这样。

0
string assignedValue = string.Empty; 
assignedValue = string.IsNullOrEmpty(txtpictext.Text) ? DBNull.Value : txtpictext.Text ; 
Cmd.Parameters.AddWithValue("@pictext", assignedValue); 
Conn.Open(); 
Cmd.ExecuteNonQuery();