Convert DataColumn.DataType to SqlDbType
ado.net, c#
Solution
I found a couple of options at Dot Net Pulse and CodeProject. I eventually went with the CodeProject code converted from VB.NET to C#:
private SqlDbType GetDBType(System.Type theType)
{
System.Data.SqlClient.SqlParameter p1;
System.ComponentModel.TypeConverter tc;
p1 = new System.Data.SqlClient.SqlParameter();
tc = System.ComponentModel.TypeDescriptor.GetConverter(p1.DbType);
if (tc.CanConvertFrom(theType)) {
p1.DbType = (DbType)tc.ConvertFrom(theType.Name);
} else {
//Try brute force
try {
p1.DbType = (DbType)tc.ConvertFrom(theType.Name);
}
catch (Exception) {
//Do Nothing; will return NVarChar as default
}
}
return p1.SqlDbType;
}
I was really hoping to find that there was some hidden secret System.Data.SqlClient method to do the conversion and I was just missing it.
Problem
Is there a converter for going from DataColumn.DataType to SqlDbType? Or do I have to write a method to do it?