简体   繁体   English

为什么对 DataRow 空值的这种取消引用不会引发异常?

[英]Why does this dereference of a DataRow null value not throw an exception?

Could you please look on the code below and say why this code doesn't throw an exception when the column value is null?您能否查看下面的代码并说明为什么当列值为空时此代码不会引发异常?

DataTable table = new DataTable();
table.Columns.Add("PreviewHtml");

table.Rows.Add(new object[] { "aksdhaskldh" });
table.Rows.Add(new object[] { "129836 128o tagjk 1782 3" });
table.Rows.Add(new object[] { null });
table.Rows.Add(new object[] { "1278o36 " });


foreach (DataRow r in table.Rows)
{
     Console.WriteLine(r["PreviewHtml"].ToString());
}

It's because DBNull.ToString returns an empty string.这是因为DBNull.ToString返回一个空字符串。

DataColumn's AllowDBNull property is set to true by default, otherwise you could not add null values. DataColumn 的AllowDBNull属性默认设置为 true,否则无法添加null值。

Null values are converted to DBNull.Value , AutoIncrement columns are also incremented when null is passed. Null值转换为DBNull.ValueAutoIncrement列在传递null时也会递增。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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