99 * Authors:
1010 * 钟峰(Popeye Zhong) <zongsoft@qq.com>
1111 *
12- * Copyright (C) 2010-2020 Zongsoft Studio <http://www.zongsoft.com>
12+ * Copyright (C) 2010-2026 Zongsoft Studio <http://www.zongsoft.com>
1313 *
1414 * This file is part of Zongsoft.Core library.
1515 *
@@ -76,6 +76,11 @@ static DataRecordExtension()
7676 RecordGetterTemplate < float > . Get = new Func < IDataRecord , int , float > ( ( record , ordinal ) => record . GetFloat ( ordinal ) ) ;
7777 RecordGetterTemplate < float ? > . Get = new Func < IDataRecord , int , float ? > ( ( record , ordinal ) => record . IsDBNull ( ordinal ) ? null : ( float ? ) record . GetFloat ( ordinal ) ) ;
7878
79+ RecordGetterTemplate < DateOnly > . Get = new Func < IDataRecord , int , DateOnly > ( ( record , ordinal ) => DateOnly . FromDateTime ( record . GetDateTime ( ordinal ) ) ) ;
80+ RecordGetterTemplate < DateOnly ? > . Get = new Func < IDataRecord , int , DateOnly ? > ( ( record , ordinal ) => record . IsDBNull ( ordinal ) ? null : DateOnly . FromDateTime ( record . GetDateTime ( ordinal ) ) ) ;
81+ RecordGetterTemplate < TimeOnly > . Get = new Func < IDataRecord , int , TimeOnly > ( ( record , ordinal ) => TimeOnly . FromDateTime ( record . GetDateTime ( ordinal ) ) ) ;
82+ RecordGetterTemplate < TimeOnly ? > . Get = new Func < IDataRecord , int , TimeOnly ? > ( ( record , ordinal ) => record . IsDBNull ( ordinal ) ? null : TimeOnly . FromDateTime ( record . GetDateTime ( ordinal ) ) ) ;
83+
7984 RecordGetterTemplate < DateTime > . Get = new Func < IDataRecord , int , DateTime > ( ( record , ordinal ) => record . GetDateTime ( ordinal ) ) ;
8085 RecordGetterTemplate < DateTime ? > . Get = new Func < IDataRecord , int , DateTime ? > ( ( record , ordinal ) => record . IsDBNull ( ordinal ) ? null : ( DateTime ? ) record . GetDateTime ( ordinal ) ) ;
8186 RecordGetterTemplate < DateTimeOffset > . Get = new Func < IDataRecord , int , DateTimeOffset > ( ( record , ordinal ) => ( DateTimeOffset ) record . GetDateTime ( ordinal ) ) ;
@@ -90,9 +95,38 @@ static DataRecordExtension()
9095 #endregion
9196
9297 #region 扩展方法
93- public static T GetValue < T > ( this IDataRecord record , int ordinal )
98+ public static T GetValue < T > ( this IDataRecord record , int ordinal ) => RecordGetterTemplate < T > . Get ( record , ordinal ) ;
99+ public static object GetValue ( this IDataRecord record , int ordinal , DbType type , bool nullable = false )
94100 {
95- return RecordGetterTemplate < T > . Get ( record , ordinal ) ;
101+ if ( nullable && record . IsDBNull ( ordinal ) )
102+ return null ;
103+
104+ return type switch
105+ {
106+ DbType . String or DbType . AnsiString => record . GetString ( ordinal ) ,
107+ DbType . StringFixedLength or DbType . AnsiStringFixedLength => record . GetString ( ordinal ) ,
108+ DbType . Boolean => record . GetValue < bool > ( ordinal ) ,
109+ DbType . Byte => record . GetValue < byte > ( ordinal ) ,
110+ DbType . SByte => record . GetValue < sbyte > ( ordinal ) ,
111+ DbType . Int16 => record . GetValue < short > ( ordinal ) ,
112+ DbType . Int32 => record . GetValue < int > ( ordinal ) ,
113+ DbType . Int64 => record . GetValue < long > ( ordinal ) ,
114+ DbType . UInt16 => record . GetValue < ushort > ( ordinal ) ,
115+ DbType . UInt32 => record . GetValue < uint > ( ordinal ) ,
116+ DbType . UInt64 => record . GetValue < ulong > ( ordinal ) ,
117+ DbType . Single => record . GetValue < float > ( ordinal ) ,
118+ DbType . Double => record . GetValue < double > ( ordinal ) ,
119+ DbType . Decimal or DbType . Currency => record . GetValue < decimal > ( ordinal ) ,
120+ DbType . Date => record . GetValue < DateTime > ( ordinal ) ,
121+ DbType . Time => record . GetValue < DateTime > ( ordinal ) ,
122+ DbType . DateTime or DbType . DateTime2 => record . GetValue < DateTime > ( ordinal ) ,
123+ DbType . DateTimeOffset => record . GetValue < DateTimeOffset > ( ordinal ) ,
124+ DbType . Binary => record . GetValue < byte [ ] > ( ordinal ) ,
125+ DbType . Guid => record . GetValue < Guid > ( ordinal ) ,
126+ DbType . Xml => record . GetValue < string > ( ordinal ) ,
127+ DbType . Object => record . GetValue ( ordinal ) ,
128+ _ => record . GetValue ( ordinal ) ,
129+ } ;
96130 }
97131
98132 public static bool TryGetOrdinal ( this IDataRecord record , string name , out int ordinal ) => TryGetOrdinal ( record , name , StringComparison . InvariantCultureIgnoreCase , out ordinal ) ;
0 commit comments