2017-12-18 261 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();