[英]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.Value
, AutoIncrement列在传递null
时也会递增。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.