处理 SQL Server 和强类型数据集中的 null GUID

发布于 2024-07-19 01:15:40 字数 520 浏览 2 评论 0原文

我在 SQL Server 中有一个表:

Categories
--------------
CategoryID (uniqueidentifier)
ParentCategoryID (uniqueidentifier) allow nulls

ParentCategoryID 旨在保存 CategoryID 中的值以指示哪个类别是父类别。 如果它没有父级(即它是顶级类别),则 ParentCategoryID 应为 null。

我正在使用强类型数据集(表适配器),并且对于 ParentCategoryID 的属性,它不允许它为空。 我尝试更改类型化数据集中字段的属性,但它说尝试将 guid 设置为“空”或“无”是无效的。 唯一的选择是抛出 null 异常。 这会导致错误:

表“Categories”中列“ParentCategoryID”的值为 DBNull。

是这样吗?或者在使用类型化数据集时有没有办法处理 null GUID/uniqueidentifiers?

I have a table in SQL server:

Categories
--------------
CategoryID (uniqueidentifier)
ParentCategoryID (uniqueidentifier) allow nulls

ParentCategoryID is meant to hold a value in CategoryID to indicate which category is the parent. If it has no parent (i.e. it's a top category) then ParentCategoryID should be null.

I'm using strongly typed datasets (table adapters) and for the properties for ParentCategoryID it does not allow it to be null. I've tried to change the properties for the field in the typed dataset but it says trying to make a guid "empty" or "nothing" is not valid. The only option is to throw an exception on null. This results in an error:

The value for column 'ParentCategoryID ' in table 'Categories' is DBNull.

Is this how it is, or is there a way to handle null GUID/uniqueidentifiers when using typed datasets?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撕心裂肺的伤痛 2024-07-26 01:15:40

如果您已使用 Visual Studio 生成器并且已正确检测到表的所有内容,则可为 null 的列将在强类型 DataRow 上生成以下内容:

  1. 为表命名的公共属性 列名称(“ParentCategoryID”)
  2. 检测到的公共方法一个 null 条目 ("bool IsParentCategoryIDNull()")
  3. 一个公共方法,用于 "nulls" 条目 ("void SetParentCategoryIDNull()")

假设您的强类型表名为“My”(生成 MyDataTableMyDataRow),您的 DataSet 名为 MyDataSetType,实例名为 myDataSet

MyDataSetType.MyRow row = myDataSet.My.NewMyRow();
row.ParentCategoryID = Guid.Empty; //OPTION 1: explicitly set GUID
row.SetParentCategoryIDNull(); //OPTION 2: explicitly set Null
myDataSet.My.AddMyRow(row);

您还可以查看实现 SetParentCategoryID 以查看用于执行“nulling”的内容。

此外,要检测“空 GUID”:

if (row.IsParentCategoryIDNull())
{
  //Do something spectacular
}

现在您有三种不同类型的值来表示状态:

  1. 数据库/数据集中的空条目(无父类别)
  2. 数据库中的非空条目(可能是父类别)
  3. 非空条目在数据库中,这是空的(Guid.Empty)guid(???)

当我第一次遇到这个问题时,我认为应该使用Guid.Empty来表示数据库中的空条目,但这需要对 guid 类型进行自定义处理。 使用包装器函数,强类型数据集可以基于结构样式类型对任意数量的可为空列提供一致的处理。

If you have used the Visual Studio generators and everything is detected properly for your table, then a nullable column will generate the following on your strongly typed DataRow:

  1. A public property named for the table Column name ("ParentCategoryID")
  2. A public method that detects a null entry ("bool IsParentCategoryIDNull()")
  3. A public method that "nulls" the entry ("void SetParentCategoryIDNull()")

Given that your strongly typed table is named "My" (Generates MyDataTable and MyDataRow), your DataSet is named MyDataSetType, and the instance is named myDataSet:

MyDataSetType.MyRow row = myDataSet.My.NewMyRow();
row.ParentCategoryID = Guid.Empty; //OPTION 1: explicitly set GUID
row.SetParentCategoryIDNull(); //OPTION 2: explicitly set Null
myDataSet.My.AddMyRow(row);

You can also look at the implementation of SetParentCategoryID to see what is used to do a "nulling".

Further, to detect a "null guid":

if (row.IsParentCategoryIDNull())
{
  //Do something spectacular
}

So now you have three different types of values to represent state:

  1. null entry in database/dataset (no parent category)
  2. non-null entry in database (parent category, presumably)
  3. non-null entry in database that is the empty (Guid.Empty) guid (???)

When i first ran into this problem, I thought Guid.Empty should have been used to represent a null entry in the database, but that would have required custom handling of the guid type. Using the wrapper functions, the strongly typed dataset can provide consistent handling of any number of nullable columns based on struct-style types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文