久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法

C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
本文介紹了C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 DateTime?,我正嘗試使用 DbParameter 將其插入到字段中.我正在像這樣創建參數:

I've got a DateTime? that I'm trying to insert into a field using a DbParameter. I'm creating the parameter like so:

DbParameter datePrm = updateStmt.CreateParameter();
datePrm.ParameterName = "@change_date";

然后我想將 DateTime? 的值放入 dataPrm.Value 中,同時考慮 nulls.

And then I want to put the value of the DateTime? into the dataPrm.Value while accounting for nulls.

我最初以為我會很聰明:

I thought initially I'd be clever:

datePrm.Value = nullableDate ?? DBNull.Value;

但由于錯誤而失敗

運算符??"不能應用于System.DateTime?"類型的操作數和'System.DBNull'

Operator '??' cannot be applied to operands of type 'System.DateTime?' and 'System.DBNull'

所以我想這只有在第二個參數是第一個參數的不可為空版本時才有效.然后我去了:

So I guess that only works if the second argument is a non-nullable version of the first argument. So then I went for:

datePrm.Value = nullableDate.HasValue ? nullableDate.Value : DBNull.Value;

但這也不起作用:

無法確定條件表達式的類型,因為'System.DateTime'和'System.DBNull'之間沒有隱式轉換

Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and 'System.DBNull'

但我不想在這些類型之間轉換!

But I don't want to convert between those types!

到目前為止,我唯一能開始工作的是:

So far the only thing I can get to work is:

if (nullableDate.HasValue)
  datePrm.Value = nullableDate.Value;
else
  datePrm.Value = DBNull.Value;

這真的是我寫這篇文章的唯一方式嗎?有沒有辦法讓單行使用三元運算符工作?

Is that really the only way I can write this? Is there a way to get a one-liner using the ternary operator to work?

更新:我真的不明白為什么 ??版本不起作用.MSDN 說:

Update: I don't really get why the ?? version doesn't work. MSDN says:

??如果不為空,運算符返回左操作數,否則返回右操作數.

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

這正是我想要的!

更新 2: 嗯,最后很明顯:

datePrm.Value = nullableDate ?? (object)DBNull.Value;

推薦答案

啊哈!我找到了比@Trebz 更有效的解決方案!

Ah ha! I found an even more efficient solution than @Trebz's!

datePrm.Value = nullableDate ?? (object)DBNull.Value;

這篇關于C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
GetType on Nullable Boolean(可空布爾值上的 GetType)
主站蜘蛛池模板: 亚洲一区国产 | 日韩av网站在线观看 | 懂色av蜜臀av粉嫩av分享 | 天天射天天射 | 在线免费国产 | 91成人免费版 | 亚洲一级二级三级 | 中文字幕国产一区 | 久久国产精 | 日韩网站免费观看 | 日本不卡在线视频 | 黄在线观看 | 免费成人毛片 | 在线免费黄色网址 | www.av在线播放 | 四虎视频在线观看 | 国产福利小视频 | 欧美三级在线看 | 日本一区二区三区四区五区 | 一区二区三区国产视频 | 亚洲一区高清 | 欧美一区二区三区免费 | 一区二区三区黄色 | 亚洲黄色在线观看 | 操操操操操操 | 99福利| 手机av免费 | 日日av| 欧美一级在线视频 | 日韩视频在线观看 | 日韩精品在线一区二区 | 亚洲黄色三级 | 97人人爱| 成人午夜在线 | www.久草.com | 日本视频www | 中国毛片视频 | 成人午夜小视频 | 日韩亚洲一区二区 | 精品久久久久久久久久久久久久久久 | 九九在线视频 |