C# 即使 AllowDBNull = False,数据集也允许 Null 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/788227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dataset allowing Null values even when AllowDBNull = False?
提问by virtualmic
I have designed a dataset using VS2008 dataset designer. In one of the datatables, I have set "AllowDBNull" property of most of the columns to be False. However, still if I create a DataRow containing null values for these columns, this datatable accepts this row, without any error.
我使用 VS2008 数据集设计器设计了一个数据集。在其中一个数据表中,我已将大多数列的“AllowDBNull”属性设置为 False。但是,如果我为这些列创建一个包含空值的 DataRow,这个数据表仍然接受这一行,没有任何错误。
Am I not understanding something here? Please advice. Thank you.
我在这里不明白吗?请指教。谢谢你。
EditMike Spross' excellent explanation however, brings forth another question. How do we check text fields if they are System.DBNull? It is surprising that DataSets are not considering a string "" as System.DBNull and throwing an exception. Or is it not?
然而,EditMike Spross 的出色解释带来了另一个问题。如果它们是 System.DBNull,我们如何检查文本字段?令人惊讶的是,DataSet 没有将字符串 "" 视为 System.DBNull 并抛出异常。或者不是?
EditI think I have found the problem and reason. I am initializing a new row of the DataTable, before filling in the values to that row. While initializing the row, default value for string, ie, "" might be being filled in that column. I think that's it? Any ideas about this?
编辑我想我已经找到了问题和原因。在将值填充到该行之前,我正在初始化 DataTable 的新行。在初始化行时,可能会在该列中填充字符串的默认值,即“”。我想是这样吗?关于这个的任何想?
采纳答案by Mike Spross
The short answer is:
简短的回答是:
System.DBNull.Value != null
System.DBNull.Value != null
The longer answer is:
更长的答案是:
In C#, the concept of a NULL
value in SQL is represented by the Value
property of the System.DBNull
class. When dealing with a database, the more familiar C# null
doesn't actually mean "null value."
在 C# 中,NULL
SQL 中值的概念由类的Value
属性表示System.DBNull
。在处理数据库时,更熟悉的 C#null
实际上并不意味着“空值”。
When you set a database column to null
, ADO.NET will initialize the column to whatever the default value is for that column (for example, an int
column would be initialized to 0). That is, using null
can actually cause a non-null value to end up in the database, and therefore you won't get an error.
当您将数据库列设置为 时null
,ADO.NET 会将列初始化为该列的任何默认值(例如,将int
列初始化为 0)。也就是说,使用null
实际上会导致非空值出现在数据库中,因此您不会收到错误消息。
If you instead set a database column to System.DBNull.Value
, the column will actually be set to NULL
. This is the situation that AllowDBNulls == false
will prevent you from doing.
如果您改为将数据库列设置为System.DBNull.Value
,则该列实际上将设置为NULL
。这种情况AllowDBNulls == false
会阻止你这样做。
回答by Kirtan
Are you exactly assigning NULL values or an empty string to those columns? If you don't assign any value to a column, it will default to NULL (if a DEFAULT constraint is not imposed). Else you can assign a NULL value by doing -
您是否准确地为这些列分配了 NULL 值或空字符串?如果您没有为列分配任何值,它将默认为 NULL(如果未施加 DEFAULT 约束)。否则,您可以通过执行以下操作来分配 NULL 值 -
ds.Tables[0].Rows[0]["Col"] = null;
If you are assigning an Empty string to those columns, it's not equal to NULL.
如果您为这些列分配空字符串,则它不等于 NULL。
And if you have a NULL value in a column which has been marked as NOT NULLABLE, it will throw an error -
如果您在标记为 NOT NULLABLE 的列中有一个 NULL 值,它会抛出一个错误 -
Column 'Col1' does not allow nulls.
列“Col1”不允许空值。
EDIT:
编辑:
By NOT NULLABLE, I mean AllowDBNull = false.
NOT NULLABLE 是指 AllowDBNull = false。
Your code seems correct. Can you try trimming the text?
你的代码似乎是正确的。您可以尝试修剪文本吗?
Here's the whole code -
这是整个代码 -
DataTable dt = new DataTable();
DataColumn column = new DataColumn("Col1");
column.AllowDBNull = false;
dt.Columns.Add(column);
DataRow dr = dt.NewRow();
dr["Col1"] = null;
dt.Rows.Add(dr);
回答by Sven Künzler
Regarding your "bonus" ;-) question: NULL (no string) and "" (empty string) are two different things. So it's perfectly reasonable to treat them differently. It's the distinction between null and DBNull that is messing things up. If nullable types had been available at the time of designing ADO.NET, things probably would be a lot easier. But before .NET 2.0, there was no way to represent e.g. a "null integer".
关于您的“奖金”;-) 问题:NULL(无字符串)和“”(空字符串)是两种不同的东西。所以区别对待它们是完全合理的。正是 null 和 DBNull 之间的区别让事情变得一团糟。如果在设计 ADO.NET 时可以使用可空类型,事情可能会容易得多。但是在 .NET 2.0 之前,没有办表示例如“空整数”。