SqlDataReader.GetSqlType() of column
Here's a way to figure out the SQL type of a column:
public static bool IsColumnOfType(
int ordinal,
string wantedSQLType,
SqlDataReader reader)
{
DataTable schema = reader.GetSchemaTable();
DataColumn dbType = schema.Columns["ProviderSpecificDataType"];
DataRow fieldInfo = schema.Rows[ordinal];
return fieldInfo[dbType].ToString() == wantedSQLType;
}
Or, using extension methods, we can write:
public static string GetSqlType(this SqlDataReader reader, int ordinal)
{
DataTable schema = reader.GetSchemaTable();
DataColumn dbType = schema.Columns["ProviderSpecificDataType"];
DataRow fieldInfo = schema.Rows[ordinal];
// You can also parse it to the SqlDBType enum
return fieldInfo[dbType].ToString();
}
public static bool IsOfSqlType(
this SqlDataReader reader,
int ordinal,
string wantedSQLType)
{
return reader.GetSqlType(ordinal) == wantedSQLType;
}
1.
Stephen Hill on January 09, 2009 at 09:51 am
Hi Sadhana,
Thanks for this. It should be something I'll be in the near future.
Just a few side notes:
Keep up the good posts.
Cheers Stephen