> ' Return a property of a field > ' expC1 Field > Public Function GetColumnProperty(ByVal tcField As String, ByVal tcProperty As String) As Object > Dim lcFieldName As String = "" > Dim loRow As DataRow = Nothing > > ' Trim just in case > tcField = Trim(tcField) > > ' Locate the field > For Each loRow In oDataTable.Rows > lcFieldName = loRow("ColumnName") > > ' If this is the field > If UCase(lcFieldName) = UCase(tcField) Then > > ' This is an example to get the type for the column > lcType = loRow("DataType") > > End If > > Next >I have the following method in our code:
/// <summary> /// Returns SqlDbType based on the Type /// </summary> /// <param name="type"></param> /// <returns></returns> public SqlDbType GetDBType(Type type) { // Original: http://www.codeproject.com/Articles/16706/Convert-System-Type-to-SqlDbType // Modified to handle additional types without raising an exception: http://stackoverflow.com/questions/14422786/alternative-to-reflection SqlParameter parameter = new SqlParameter(); System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(parameter.DbType); if (typeConverter.CanConvertFrom(type)) { parameter.DbType = (DbType)typeConverter.ConvertFrom(type.Name); } else { switch (type.Name) { case "Char": parameter.SqlDbType = SqlDbType.Char; break; case "SByte": parameter.SqlDbType = SqlDbType.SmallInt; break; case "UInt16": parameter.SqlDbType = SqlDbType.SmallInt; break; case "UInt32": parameter.SqlDbType = SqlDbType.Int; break; case "UInt64": parameter.SqlDbType = SqlDbType.Decimal; break; case "Byte[]": parameter.SqlDbType = SqlDbType.Binary; break; default: // If none of the above types, try to forcefully convert try { parameter.DbType = (DbType)typeConverter.ConvertFrom(type.Name); } catch (Exception ex) { String error = ex.ToString(); } break; } } return parameter.SqlDbType; }