久久久久久久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 將其插入到字段中.我正在像這樣創(chuàng)建參數(shù):

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?"類型的操作數(shù)和'System.DBNull'

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

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

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'之間沒有隱式轉(zhuǎn)換

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

但我不想在這些類型之間轉(zhuǎn)換!

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:

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

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;

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

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

相關(guān)文檔推薦

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#中將空值設(shè)置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調(diào)用)
GetType on Nullable Boolean(可空布爾值上的 GetType)
主站蜘蛛池模板: 久久国产美女视频 | 久久91精品久久久久久9鸭 | 午夜资源 | 99精品欧美 | 日韩欧美国产一区二区三区 | 男人久久天堂 | 欧美一区二区三区在线看 | 一区二区三区四区在线播放 | 欧美日韩亚洲在线 | 欧美激情综合 | 精品国产一级 | 久久er精品 | 欧美中文字幕一区二区 | 99免费精品 | 精品国产91 | 亚洲高清在线 | 亚洲成人动漫在线观看 | 久久99视频免费观看 | 亚洲乱码一区二区三区在线观看 | 91色在线视频 | 亚洲一区免费 | 亚洲欧美日韩在线一区二区 | 精品日本中文字幕 | 国产999精品久久久 午夜天堂精品久久久久 | 一级在线视频 | 久久亚洲一区二区 | 91人人视频在线观看 | 久久精品久久久 | 欧美成人h版在线观看 | 中文在线视频 | 日日操av| 久久99精品久久久久久青青日本 | 看羞羞视频免费 | 91精品国产一区二区三区蜜臀 | 中文字幕国产一区 | 欧美视频一区二区三区 | 日日干天天操 | 亚洲成网站 | 久久国| 久热精品在线 | 中文字幕久久久 |