2

Is there any way to do something like this:

(SqlDbType.Int).Parse(dtbDataTable.Rows[0]["Id"])
7
  • What is the value of dtbDataTable.Rows[0]["Id"]? Commented Jun 28, 2013 at 15:40
  • Is "Id" actually a data type identifier, or are you trying to figure out the data of the column Id? Commented Jun 28, 2013 at 15:41
  • Can you explain why do you want to output the ["Id"] field to the enumeration of type SqlDbType? Don't you want to do Int.Parse(dtbDataTable.Rows[0]["Id"].ToString()) or simply (int)dtbDataTable.Rows[0]["Id"] if this is already an integer? Commented Jun 28, 2013 at 15:42
  • It is not a typed DataTable, because it is dynamic...The Id constains a number like 150 Commented Jun 28, 2013 at 15:43
  • @saamorim, i dont want to let the conversion error of any data occurs inside the stored procedure, because i wont know what column is in the wrong type, so i want to make the tryparse at setting time Commented Jun 28, 2013 at 15:46

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

Comments

0

Unfortunately no. SqlDbType is an enum, so (SqlDbType.Int) actually boils down to an integer value, not a type. The only way I can think of to do this is some sort of switch statement:

switch (SqlDbType dbType)
{
    case SqlDbType.Int:
       int value = Int32.Parse(dtbDataTable.Rows[0]["Id"]);
       //Do stuff with this value
    //repeat for other types
 }

2 Comments

It is a nice way to solve my problem @Omada, i use it inside my Framework to another things, but it reduces a little of performance because of existing a lot of parameters sometimes
If exists some way of not doing the parse and the switch case, it would look better
0

I think that would be tough to do, and it's not the most readable way. I handle this via extension methods to help with TinyInt, SmallInt, and nullable values across the board. E.g.:

using (var dr = new SafeDataReader(cmd.ExecuteReader()) {
  while (dr.Read()) {
   int? id = dr.GetNullableIntFromSqlTinyInt(0);
   // Other stuff like that to handle type conversions
  }
}

SafeDataReader is part of the CSLA business object framework, but you could implement your own DataReader if you would like. It's a lot more legible and encapsulates all the parsing logic behind the scenes to the extension method.

2 Comments

It is not applicable to Int only, there is various datatypes @Joe, this solution looks specific to Int/Tinyint types
This example yes, so you'd have a library of such extension methods of GetXFromSqlY(). We have about 15 or so of these.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.