2011-10-31 8 views

답변

58

공통적 인 방법은 지원되는 모든 유형 (다른 커넥터/제공자는 서로 다른 유형을 지원함)과 함께 유형 맵을 명시하는 것입니다. 여기 Dapper의 형태 맵입니다 :

typeMap = new Dictionary<Type, DbType>(); 
typeMap[typeof(byte)] = DbType.Byte; 
typeMap[typeof(sbyte)] = DbType.SByte; 
typeMap[typeof(short)] = DbType.Int16; 
typeMap[typeof(ushort)] = DbType.UInt16; 
typeMap[typeof(int)] = DbType.Int32; 
typeMap[typeof(uint)] = DbType.UInt32; 
typeMap[typeof(long)] = DbType.Int64; 
typeMap[typeof(ulong)] = DbType.UInt64; 
typeMap[typeof(float)] = DbType.Single; 
typeMap[typeof(double)] = DbType.Double; 
typeMap[typeof(decimal)] = DbType.Decimal; 
typeMap[typeof(bool)] = DbType.Boolean; 
typeMap[typeof(string)] = DbType.String; 
typeMap[typeof(char)] = DbType.StringFixedLength; 
typeMap[typeof(Guid)] = DbType.Guid; 
typeMap[typeof(DateTime)] = DbType.DateTime; 
typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset; 
typeMap[typeof(byte[])] = DbType.Binary; 
typeMap[typeof(byte?)] = DbType.Byte; 
typeMap[typeof(sbyte?)] = DbType.SByte; 
typeMap[typeof(short?)] = DbType.Int16; 
typeMap[typeof(ushort?)] = DbType.UInt16; 
typeMap[typeof(int?)] = DbType.Int32; 
typeMap[typeof(uint?)] = DbType.UInt32; 
typeMap[typeof(long?)] = DbType.Int64; 
typeMap[typeof(ulong?)] = DbType.UInt64; 
typeMap[typeof(float?)] = DbType.Single; 
typeMap[typeof(double?)] = DbType.Double; 
typeMap[typeof(decimal?)] = DbType.Decimal; 
typeMap[typeof(bool?)] = DbType.Boolean; 
typeMap[typeof(char?)] = DbType.StringFixedLength; 
typeMap[typeof(Guid?)] = DbType.Guid; 
typeMap[typeof(DateTime?)] = DbType.DateTime; 
typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset; 
typeMap[typeof(System.Data.Linq.Binary)] = DbType.Binary; 

는 관련 DbType과를 얻으려면, 당신이해야 할 모든은 다음과 같습니다

var type = typeMap[typeof(string)]; // returns DbType.String 
+0

이것은 나를 위해 좋아 보인다. 'Type.GetGenericTypeDefinition'과'Type.GetGenericArguments'는 Nullable 값과 기본 타입을 해결하는 데 도움이 될 것이므로 Nullable 버전에서는 절 필요가 없다고 생각합니다. – Mike

10

설명서를 보면 - SQL Server Data Type Mappings (ADO.NET)입니다.

다른 공급자의 매핑은 documented입니다.

이 정보는 변환기 작성에 필요한 충분한 정보를 제공합니다.

+0

하지만 공급자에 무관심한 방법이 있습니까? – Mike

+0

@ 마이크 - 나는있을 것이라고 상상할 수 없다. 다른 데이터베이스와 제공자는 다른 방식으로 구현됩니다. 가장 가까운 매핑을 문서화 된대로 찾아 공통 클래스에서 사용할 수 있습니다. – Oded

+0

나는 이해한다. 왜 Framework에 DbType이 있는지 궁금합니다. 그 값은 BCL 타입과 매우 비슷하므로 기본 매핑이 있어야한다고 생각했습니다. – Mike

1

난 당신이 매핑 자신을해야, 자동화 된 로직을 인식하지 오전 왜냐하면 그것들은 다른 유형이고 .NET Framework는 당신을 위해서만 이것을 할 수 없기 때문입니다.

여기에 전체 매핑 테이블을 참조하십시오 SQL Server Data Type Mappings (ADO.NET) 당신은 또한 .NET 데이터 공급자에 따라 유사한 테이블이있을 수 있습니다 오라클, MySQL은, SQLite는 다른 엔진이 상상할 수/

7

당신은 DbTypeTypeCode을 변환 할 수 있습니다 연결 방법 ConvertTypeCodeToDbType을 사용하여 System.Web.UI.WebControls.Parameter 클래스 : Parameter.ConvertTypeCodeToDbType Method. TypeCode를 얻으려면 Type.GetTypeCode(Type type) 메서드를 사용할 수 있습니다.

+10

왜 이건 'System.Web'에 있습니까? – talles

관련 문제