SQL/.NET TableAdapters – How do I strongly-type calculated columns in either the database or the DataSet?

SQL/.NET TableAdapters – How do I strongly-type calculated columns in either the database or the DataSet?

能否归途做我良人 发布于 2021-11-26 字数 1134 浏览 794 回复 2 原文

We use calculated columns in a few SQL Server 2005 tables which always return data of a specific type (bit, varchar(50), etc...).

These tables are consumed by a .NET data layer using strongly-typed datasets and tableadapters, however since the calculated columns are not constrained to a specific type (we always return a specific type, but we could return anything) .NET does not generate strongly-typed columns.

Three questions here:
- The calcuated columns typically check that related records have specific values, or they return a single value from a related XML document using XPath. Are calculated columns the proper tool for this, or is there a better way to return these related pieces of data?

  • My searches say no, but is it possible to strongly-type these columns at the database itself?

  • Is there a workaround inside of the Visual Studio DataSet designer that would allow me to set the proper types and maintain them across DataSet/TableAdapter updates (since the Designer will query the database to update it's schema)?

Thanks!

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

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

评论(2

半葬歌 2022-06-07 2 楼

First, they do have a type.

Second, the SQL Server will tell clients the type if the client requests it, whether its a C API client, a C# client, a JDBC client, etc. Otherwise, the clients can't correctly cat the type.

Third, you can find out what type they have by looking at the information_schema.columns (or syscolumns) row for the calculated columns.

Fourth, you can use the CAST operator to cast these columns to any type compatible with their actual types.

似梦非梦 2022-06-07 1 楼

In the queries that you use to return these values, you can use the SQL CAST() function to cast them as whatever datatype you want. I believe .NET should recognize the casted datatype and put that type in the dataset.