简体   繁体   English

使用.Net SqlClient连接到SQL Azure DB,是否可以为架构中的数据类型获取DBNull对象?

[英]Using .Net SqlClient to connect to SQL Azure DB, is it possible to get a DBNull object for the data type in schema?

We are using classes under System.Data.SqlClient to connect to Azure SQL databases. 我们正在使用System.Data.SqlClient下的类连接到Azure SQL数据库。 We process the data type by executing a query (or a stored procedure) and then performing some operations like below on the resulting IDataReader object. 我们通过执行查询(或存储过程),然后对所得的IDataReader对象执行以下类似操作来处理数据类型。

var schema = reader.GetSchemaTable();
for (int i = 0; i < schema.Rows.Count; i++)
{
    Type type = (Type) schema.Rows[i]["DataType"];
    // do something for the type
}

This code runs against our users' databases, and for one of them, we get the following error: 此代码针对我们用户的数据库运行,对于其中一个,我们收到以下错误:

System.InvalidCastException
Unable to cast object of type 'System.DBNull' to type 'System.Type'.

Since we don't easily get access to the user's database servers, we are having difficulty in reproducing this error. 由于我们不容易访问用户的数据库服务器,因此很难再现此错误。 I have tried all possible data types I can use to create a table in SQL Azure, but all of them are able to be mapped to some Type in C#. 我尝试了所有可用于在SQL Azure中创建表的数据类型,但是所有这些数据类型都可以映射到C#中的某些Type So now I'm very confused, for what kind of database table in SQL Azure would I get an object of System.DBNull for the data type of the schema table? 因此,现在我很困惑,对于SQL Azure中的哪种数据库表,我将为架构表的数据类型获得System.DBNull对象?

Any ideas? 有任何想法吗? Thanks. 谢谢。

We have found the problem. 我们发现了问题。 The data type is geography(or geometry), those are SQL CLR types, it requires the installation of certain assembly (usually installed along with SQL Server) in order for your code to get it's .NET runtime type. 数据类型是地理(或几何),它们是SQL CLR类型,它需要安装某些程序集(通常与SQL Server一起安装),以便您的代码获得它的.NET运行时类型。 Otherwise DBNull is returned as data type. 否则,将DBNull作为数据类型返回。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Functions运行时2-使用SqlClient的SQL连接 - Azure Functions runtime 2 - SQL Connections using SqlClient 无法使用.net在给定的数据库连接中连接到表Azure SQL - Can't Connect to the table azure SQL in given DB connection using .net 无法使用C#控制台应用程序(使用System.Data)连接到Azure SQL DB - Cannot connect to Azure SQL DB using C# Console App (using System.Data) 无法将类型为“System.Data.SqlClient.SqlInternalConnectionTds”的对象强制转换为“System.Data.SqlClient.SqlInternalConnectionSmi” - Unable to cast object of type 'System.Data.SqlClient.SqlInternalConnectionTds' to type 'System.Data.SqlClient.SqlInternalConnectionSmi' 不能将对象从 DBNull 强制转换为 Double 数据类型的其他类型 - Object cannot be cast from DBNull to other types for Double Data Type 无法在桌面应用程序中使用SqlClient连接到SQL Server - Cannot connect to SQL Server using SqlClient in a desktop application 使用 c# 连接到 Azure VM 的 SQL DB - Connect to SQL DB of Azure VM using c# 将类型&#39;object&#39;转换为&#39;System.Data.SqlClient.SqlCommand错误 - convert type 'object' to 'System.Data.SqlClient.SqlCommand Error netcore3.1 System.Data.SqlClient 上的 Azure 函数 3.0.1:“System.Data.SqlClient.TdsParser”的类型初始值设定项引发异常 - Azure Function 3.0.1 on netcore3.1 System.Data.SqlClient: The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception 在数据表中转换项目的数据类型时如何传递DBNull:无法将对象从DBNull转换为其他类型 - How to pass DBNull when convert data type of item in DataTable: Object can't cast from DBNull to other types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM