Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DataType for VarChar
Message
 
 
To
31/03/2014 17:23:37
General information
Forum:
ASP.NET
Category:
Databases
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01597800
Message ID:
01597806
Views:
32
>>There is SqlDbType.VarChar data type, but System.String type. So, for all char data it will be System.String type.
>
>Well, this causes a problem, as when I use a database which is not bound to my data dictionary, I need to rely on what the DataTable will return. So, I need to be able to differentiate between a string and a varchar.
>
>For example, this is a method I have that I call when I need to know the data type of a column, if I do not have my data dictionary to rely on. This assumes oDataTable is already available. So, I pass the field, in this case a column formely known in SQL Server, and the property I need to get info from. So, the one that is of interest here is DataType. So, I can call the method here with "FirstName", "DataType" and this will return System.String, for example, if FirstName is a column of type Character in SQL Server. But, I need a way to differentiate that from a VarChar. This seems impossible I would assume.
>
>
>    ' 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;
        }
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform