2013-06-28 2 views
2

이런 식으로 뭔가 할 수있는 방법이 있나요 :C# 구문 분석 SqlDbType Convertion

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

TKS는

+0

'dtbDataTable.Rows [0] [ "Id"]'의 값은 무엇입니까? – manji

+0

"Id"는 실제로 데이터 유형 식별자입니까, 아니면 열 ID의 데이터를 파악하려고합니까? – vcsjones

+0

[ "Id"] 필드를 SqlDbType 형식의 열거 형으로 출력하려는 ​​이유를 설명 할 수 있습니까? 정수형 인 경우 dtbDataTable.Rows [0] [ "Id"] Int.Parse (dtbDataTable.Rows [0] [ "Id"]. ToString()) 또는 단순히 (int) ? – 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; 
     } 
    } 

TKS를!

+0

쿨하지만 psdtParameter를 string으로 변환해야하는 이유가 없습니다 ... – Antonio

0

불행하게도. SqlDbType은 열거 형이므로 (SqlDbType.Int)은 실제로 형식이 아닌 정수 값으로 내려갑니다. 내가 그 할 어려울 것이라고 생각

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

내 문제를 해결하는 좋은 방법입니다 @ 오마다, 내 프레임 워크 내에서 다른 것들을 사용하지만, 때로는 많은 매개 변수로 인해 성능이 다소 저하됩니다. – raddesso

+0

파싱을 수행하지 않는 방법이 있다면 스위치 케이스, 그것은 더 좋아 보일 것입니다 – raddesso

0

, 그리고 가장 읽을 수있는 방법이 아니다 : 나는이 작업을 수행하는 생각할 수있는 유일한 방법은 switch 문의 일종이다. 필자는 TinyInt, SmallInt, nullable 값을 지원하기 위해 확장 메서드를 통해이를 처리합니다. 예컨대 :

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

SafeDataReader는 CSLA 비즈니스 오브젝트 프레임 워크의 한 부분이지만, 당신이 좋아하면 당신은 당신의 자신의 DataReader를 구현할 수있다. 그것은 훨씬 더 읽기 쉽고 뒤에서 모든 파싱 로직을 확장 메서드로 캡슐화합니다.

+0

그것은 Int에만 적용되지 않습니다, 다양한 데이터 유형 @Joe가 있습니다.이 솔루션은 Int/Tinyint 유형에만 해당됩니다. – raddesso

+0

이 예에서는 그렇습니다. 따라서 GetXFromSqlY(). 우리는 약 15 가지 정도를 가지고 있습니다. – Joe