vacpro
vacpro

Reputation: 31

How to read sql_variant database type in C#

Looking into System.Data.DbType there is no SqlVariant type there. SqlDataReader, for example, provides the GetString method for reading into string variable. What is the appropriate way to retrieve data from the database field of type sql_variant, presumably into object?

The aim is to read data stored as sql_variant type in database. Not to assign the value into variable of object type. I mentioned object type variable because I thing the sql_variant, if possible, would go into such type.

Upvotes: 3

Views: 5932

Answers (2)

JEuvin
JEuvin

Reputation: 1037

Sql_Variant is of type object.

Microsoft Docs

Upvotes: 1

Rob
Rob

Reputation: 45809

If you want to put the data into a variable of type object then (simplified):

object result = null;
result = reader["columnNameGoesHere"];

Should do the trick.

There's also a good explanation of the various different methods of retrieving the contents of a given columns current record in this DevHood tutorial. The summary table of data type mappings at the end may come in handy too!

Upvotes: 4

Related Questions