[英]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.