there would be a solution to create a dictionary matching sql types with .net types:
Dictionary<string, Type> sqlToNetTypes;
and populate it with all the possible types that you can find in the "DATA_TYPE" column and their .NET equivalent:
sqlToNetTypes.Add("int", typeof(int));
sqlToNetTypes.Add("varchar", typeof(sting));
sqlToNetTypes.Add("datetime", typeof(DateTime));
sqlToNetTypes.Add("bit", typeof(bool));
sqlToNetTypes.Add("numeric", typeof(float));
...
then in the helper method:
Type GetNETType(string sqlType)
{
if(sqlToNetTypes.ContainsKey(sqlType))
{
return sqlToNetTypes[sqlType];
}else
{
return typeof(object);
}
}
and use it as follows:
foreach (DataRow row in Columns.Rows)
if (row["TABLE_NAME"].ToString() == tableName)
{
if (fName == row["COLUMN_NAME"].ToString())
{
var x = GetNETType(row["DATA_TYPE"]);
}
}
source
share