[英]Why does this dereference of a DataRow null value not throw an exception?
您能否查看下面的代码并说明为什么当列值为空时此代码不会引发异常?
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());
}
这是因为DBNull.ToString
返回一个空字符串。
DataColumn 的AllowDBNull
属性默认设置为 true,否则无法添加null
值。
Null
值转换为DBNull.Value
, AutoIncrement列在传递null
时也会递增。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.