为什么 ODBC 中列模式的 DATA_TYPE 为负数?

发布于 2024-08-04 13:49:22 字数 570 浏览 1 评论 0原文

我试图允许用户从该表中选择一个包含 2 列的表以在查询中使用。

他们定义提供者和连接字符串来选择数据库。这是使用 DBProviderFactory 和 DBConnectionStringBuilder 完成的。问题似乎是使用 ODBC 提供程序时,其他提供程序(OLEDB、Sql)似乎没问题。

我查询架构以获取表。然后用户选择一张桌子。由于我只希望用户能够选择一个字符串列和一个数字列,因此我尝试根据模式中定义的类型来限制为所选表显示的列。为此,我查询 DataTypes 集合并使用 ProviderDbType 列(其中包含数字)和 DataType 列(其中包含 .Net 系统类型)来构建 DbType 到系统类型的映射。

然后,我查询用户选择的表的架构,并根据结果中的 DATA_TYPE 列查找系统类型,因此现在我可以允许用户选择 1 列(字符串)和另一列数字。我的问题是,对于大多数数据库类型,这都有效,但对于 nchar,ProviderDbType 被列为 11(在 DataTypes 架构查询中),但是当我获取表的列时,DATA_TYPE 为 -8(这不是ProviderDbType 列)。

如何正确判断列的类型?或者我做错了什么?

I am trying to allow a user to select a table a 2 columns from that table for use in a query.

They define the provider and the connection string to choose the database. This is done using the DBProviderFactory and the DBConnectionStringBuilder. The problem seems to be when using the ODBC provider, other providers (OLEDB, Sql) seem ok.

I query the schema to get the tables. The user then chooses a table. As I only want to the user to be able to choose one string column and one numeric column I am trying to restrict the columns displayed for the chosen table based on their defined type in the schema. to do this I query the DataTypes collection and use the ProviderDbType column (which contains a number) and the DataType column (which contains the .Net system type) to build a map of DbType to System types.

I then query the schema for the table the user has chosen and based on the DATA_TYPE column in the result, look up the system type, so now I can allow the user to choose 1 column which is a string and another that is numeric. My problem is that for most database types this works, but for nchar the ProviderDbType is listed as 11 (in the DataTypes schema query), but when I get the columns of the table the DATA_TYPE is -8 (which is not a number in the ProviderDbType column).

How can I determine the type of the column correctly? Or am I doing something wrong?

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

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

发布评论

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

评论(1

天涯沦落人 2024-08-11 13:49:22

来自 unixODBC 的 sqlext.h:

#define SQL_IS_POINTER                          (-4)
#define SQL_IS_UINTEGER                         (-5)
#define SQL_IS_INTEGER                          (-6)
#define SQL_IS_USMALLINT                        (-7)
#define SQL_IS_SMALLINT                         (-8)

sqlucode.h:

#define SQL_WCHAR           (-8)
#define SQL_WVARCHAR        (-9)
#define SQL_WLONGVARCHAR    (-10)
#define SQL_C_WCHAR         SQL_WCHAR

将数据传递到 ODBC 时使用这些。您可能正在查看 SMALLINT 吗?它是一个 16 位数量,与 SQL_WCHAR(Windows、.net 和 Java 中使用的常规 UTF-16 字符)相同。

From unixODBC's sqlext.h:

#define SQL_IS_POINTER                          (-4)
#define SQL_IS_UINTEGER                         (-5)
#define SQL_IS_INTEGER                          (-6)
#define SQL_IS_USMALLINT                        (-7)
#define SQL_IS_SMALLINT                         (-8)

sqlucode.h:

#define SQL_WCHAR           (-8)
#define SQL_WVARCHAR        (-9)
#define SQL_WLONGVARCHAR    (-10)
#define SQL_C_WCHAR         SQL_WCHAR

These are used when passing data into ODBC. Could it be you are looking at a SMALLINT? It's a 16-bit quantity, the same as a SQL_WCHAR (a regular UTF-16 character as used in Windows, .net and Java).

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