Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 374
vacpro Avatar asked Sep 02 '26 03:09

vacpro


2 Answers

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!

like image 65
Rob Avatar answered Sep 04 '26 16:09

Rob


Sql_Variant is of type object.

Microsoft Docs

like image 37
JEuvin Avatar answered Sep 04 '26 15:09

JEuvin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!