考虑像这样的sqlite表:
+--------------+-----------+--------------+ -----------+
| Col1(string) | Col2(int) | Col3(double) | ... |
+--------------+-----------+--------------+------------+
| A_string | B_int | C_double | ... |
+--------------+-----------+--------------+------------+
......意味着拥有更多专栏 . 目前,我们从c#中获取表中的数据,如下所示:
using( SQLiteConnection con = new SQLiteConnection( databaseConnectionString ) )
{
con.Open(); //Open connection
using( SQLiteCommand cmd = new SQLiteCommand( "SELECT * FROM table", con ) )
{
var dataTable = new DataTable();
using( var sqlDataAdapter = new SQLiteDataAdapter( cmd ) )
{
sqlDataAdapter.Fill( dataTable );
if( dataTable.Rows.Count > 0 ) //ensure, there are some results
{
foreach( DataRow row in dataTable.Rows )
{
//Now initialize the c# objects
//But we do not wannt to hardcode every cast,
//cause the type information is already in the database
//therefore the function "getColumnValueAsRuntimeType()" is used to get the runntime type
string a = getColumnValueAsRuntimeType( "Col1", row );
int b = getColumnValueAsRuntimeType( "Col2", row );
double c = getColumnValueAsRuntimeType( "Col3", row );
...
}
}
}
}
}
private dynamic getColumnValueAsRuntimeType( string columnName, DataRow row )
{
int index = row.Table.Columns.IndexOf( columnName );
Type castTo = row.Table.Columns[ index ].DataType.UnderlyingSystemType;
if( typeof( Int64 ) == castTo )
{
return Convert.ChangeType( row[ columnName ], typeof( Int32 ) );
}
return Convert.ChangeType( row[ columnName ], castTo );
}
由于以下呼吁,现在存在一些性能问题:
int index = row.Table.Columns.IndexOf( columnName );
Type castTo = row.Table.Columns[ index ].DataType.UnderlyingSystemType;
在函数“getColumnValueAsRuntimeType()”
所以我的主要问题是:
- 在c#中从sqlite数据库表中获取数据的最快/性能最佳方法是什么?没有对源代码中的每个typcast进行硬编码(我们不希望每次更改数据库类型时都重新编译源代码 . 数据库应该是主数据库) . 为什么我还要施展呢?我使用c#类访问数据库,数据应该已经输入 .
我们在这里谈论的是一个包含1000列和数百万行的sqlite表 .
1 回答
我会使用Dapper之类的东西来处理映射 . 如果创建一个与SQL语句返回的数据相对应的类,那么Dapper将分析该类,并为返回的所有行只为您执行一次列映射 .
它看起来有点像这样:
你的sql语句看起来像
通过在SQL中使用别名,您可以处理不同的命名约定 . Manually Map column names with class properties
那你就做
Dapper还可以处理嵌套对象:How do I map lists of nested objects with Dapper