이런 식으로 뭔가 할 수있는 방법이 있나요 :C# 구문 분석 SqlDbType Convertion
(SqlDbType.Int).Parse(dtbDataTable.Rows[0]["Id"])
TKS는
이런 식으로 뭔가 할 수있는 방법이 있나요 :C# 구문 분석 SqlDbType Convertion
(SqlDbType.Int).Parse(dtbDataTable.Rows[0]["Id"])
TKS는
내 해결 방법을 다시 게시 :
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를!
쿨하지만 psdtParameter를 string으로 변환해야하는 이유가 없습니다 ... – Antonio
불행하게도. 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
}
, 그리고 가장 읽을 수있는 방법이 아니다 : 나는이 작업을 수행하는 생각할 수있는 유일한 방법은 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를 구현할 수있다. 그것은 훨씬 더 읽기 쉽고 뒤에서 모든 파싱 로직을 확장 메서드로 캡슐화합니다.
'dtbDataTable.Rows [0] [ "Id"]'의 값은 무엇입니까? – manji
"Id"는 실제로 데이터 유형 식별자입니까, 아니면 열 ID의 데이터를 파악하려고합니까? – vcsjones
[ "Id"] 필드를 SqlDbType 형식의 열거 형으로 출력하려는 이유를 설명 할 수 있습니까? 정수형 인 경우 dtbDataTable.Rows [0] [ "Id"] Int.Parse (dtbDataTable.Rows [0] [ "Id"]. ToString()) 또는 단순히 (int) ? – saamorim