2013-06-28 29 views
2

有没有办法做这样的事情:C#解析SqlDbType皈依

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

韩国社交协会

+0

'dtbDataTable.Rows [0] [“Id”]'的值是多少? – manji

+0

“Id”实际上是数据类型标识符,还是试图找出列ID的数据? – vcsjones

+0

你能解释为什么你想输出[“Id”]字段到SqlDbType类型的枚举? 如果这已经是一个整数,你不想做Int.Parse(dtbDataTable.Rows [0] [“Id”] .ToString())或简单的(int)dtbDataTable.Rows [0] [“Id”] ? – saamorim

回答

1

回发我的解决方法:

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; 
     } 
    } 

韩国社交协会!

+0

很酷,但我不明白为什么你必须将psdtParameter转换为字符串... – Antonio

0

遗憾的是没有。 SqlDbType是一个枚举,因此(SqlDbType.Int)实际上归结为一个整数值,而不是一个类型。我能想到的要做到这一点的唯一方法是某种switch语句:

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

这是一个很好的方法来解决我的问题@Omada,我在我的框架内使用它来处理另一件事情,但它会降低一点性能,因为有时会存在很多参数 – raddesso

+0

如果存在某种不做解析的方式,它看起来会更好 – raddesso

0

我想这是很难做的,它不是最可读的方式。我通过扩展方法处理这个问题,以帮助TinyInt,SmallInt和可空的值。例如: -

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

SafeDataReader是里昂证券的业务对象框架的一部分,但你可以实现自己的DataReader,如果你想。它更清晰易读,并将所有解析逻辑封装在扩展方法的幕后。

+0

它仅适用于Int,有各种数据类型@Joe,这个解决方案看起来特定于Int/Tinyint类型 – raddesso

+0

这个例子是的,所以你应该有一个这样的扩展方法库GetXFromSqlY ()。我们有大约15个左右。 – Joe