3 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
基本上:沒有。
除非您禁用了 ANSI NULL 語法(請(qǐng)不要這樣做),否則您需要不同的 SQL來測(cè)試NULL. 一個(gè)常見的技巧(易于編寫,但效率不高)是作弊:
WHERE (FirstName = @FirstName OR (@FirstName IS NULL AND FirstName IS NULL))
AND (LastName = @LastName OR (@LastName IS NULL AND LastName IS NULL))
-- etc x5
(或您想要為空值執(zhí)行的任何測(cè)試) - 并使用類似的東西:
database.sqlCommand.Parameters.AddWithValue("@FirstName", txbFirstName.Text.DBNullIfEmpty());
database.sqlCommand.Parameters.AddWithValue("@LastName", txbLastName.Text.DBNullIfEmpty());
// etc x5
在哪里DBNullIfEmpty看起來像:
internal static class MyExtensionMethods
{
public static object DBNullIfEmpty(this string value)
{
if(string.IsNullOrWhiteSpace(value)) return DBNull.Value;
return value;
}
}

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以為 AddWithValue() 創(chuàng)建一個(gè)擴(kuò)展方法,并在實(shí)際值為 NULL 時(shí)傳遞一個(gè)額外的第三個(gè)參數(shù)作為您想要的值
public static class SqlParameterCollectionExtensions {
public static SqlParameter AddWithValue(this SqlParameterCollection target, string parameterName, object value, object nullValue) {
if (value == null) {
return target.AddWithValue(parameterName, nullValue);
}
return target.AddWithValue(parameterName, value);
}
}
后來這樣稱呼它
database.sqlCommand.Parameters.AddWithValue("@City", txbCity.Text, "IS NULL");

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
SQL Server 數(shù)據(jù)庫中的值對(duì)應(yīng)的參數(shù)NULL
值是DBNull.Value
。
Parameters.Add(“@NAME”, SqlDbType.<type>).Value = argument == null ? DBNull.Value : argument;
- 3 回答
- 0 關(guān)注
- 110 瀏覽
添加回答
舉報(bào)