简体   繁体   中英

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. 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.

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#. 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?

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. Otherwise DBNull is returned as data type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM