简体   繁体   中英

C# dataset and DbNull values

I've a general question - Lets say I've an SQL Server 2008 DB , and it has some nullable columns. Some of these are null and some are not.

Next, I query the DB for a DataRow in C#.

What value would these null fields have in the DataRow object? I'm seeing that they have C# "null" values, but someone here said that DbNull != C# null....

Please explain... thanks.

DBNull.Value which is not the same as null .

DBNull.Value Field (System)

With untyped datasets (in DataRow), you can test like this: DbNull.Value.Equals(dataRow["column"])

With typed datasets, for nullable columns, there are IsNull methods generated, eg myTypedDataRow.IsMyColumnNull() which by default you should test before trying to access actual myTypedDataRow.MyColumn value, because if it has a null value, it would throw an exception. This behavior can be changed by setting column's NullValue property in DataSet Designer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM