Search code examples
c#parsingcastingtype-conversionsqldbtype

C# Parse SqlDbType Conversion


Is there any way to do something like this:

(SqlDbType.Int).Parse(dtbDataTable.Rows[0]["Id"])

Solution

  • Posting back my workaround:

        public static string ParseValue(SqlDbType psdtParameter, string pstrValue, string pstrDateFormat = null)
        {
            object objReturn = new object();
            if (pstrValue != "")
            {
                switch (psdtParameter.ToString())
                {
                    case "BigInt":
                        objReturn = TypeDescriptor.GetConverter(typeof(Int64)).ConvertFromString(pstrValue);
                        break;
                    case "Bit":
                        objReturn = TypeDescriptor.GetConverter(typeof(Boolean)).ConvertFromString(pstrValue);
                        break;
                    case "NText":
                    case "NVarChar":
                    case "VarChar":
                    case "NChar":
                    case "Text":
                    case "Char":
                        objReturn = TypeDescriptor.GetConverter(typeof(String)).ConvertFromString(pstrValue);
                        break;
                    case "SmallDateTime":
                    case "DateTime":
                        objReturn = DateTime.ParseExact(pstrValue, pstrDateFormat, CultureInfo.InvariantCulture);
                        //TypeDescriptor.GetConverter(typeof(DateTime)).ConvertFromString(pstrValue);
                        break;
                    case "Money":
                    case "SmallMoney":
                    case "Decimal":
                        objReturn = TypeDescriptor.GetConverter(typeof(Decimal)).ConvertFromString(null, CultureInfo.InvariantCulture, pstrValue);
                        break;
                    case "Float":
                        objReturn = TypeDescriptor.GetConverter(typeof(Double)).ConvertFromString(pstrValue);
                        break;
                    case "Binary":
                    case "VarBinary":
                    case "Timestamp":
                    case "Image":
                        objReturn = TypeDescriptor.GetConverter(typeof(Byte[])).ConvertFromString(pstrValue);
                        break;
                    case "Int":
                        objReturn = TypeDescriptor.GetConverter(typeof(Int32)).ConvertFromString(pstrValue);
                        break;
                    case "Real":
                        objReturn = TypeDescriptor.GetConverter(typeof(Single)).ConvertFromString(pstrValue);
                        break;
                    case "SmallInt":
                        objReturn = TypeDescriptor.GetConverter(typeof(Int16)).ConvertFromString(pstrValue);
                        break;
                    case "TinyInt":
                        objReturn = TypeDescriptor.GetConverter(typeof(Byte)).ConvertFromString(pstrValue);
                        break;
                }
                return objReturn.ToString();
            }
            else
            {
                return null;
            }
        }
    

    Tks!