由于动态查询,将 SqlDataReader 转换为未知类型的 List
朋友们, 我有一个动态 sql 查询,我想执行并返回一个列表。 从我在 goole 上的大部分互联网搜索中,我发现必须知道列表的类型才能将 sqldatareader 转换为列表。 当我不知道将执行什么查询时,我该如何做到这一点。
Execute(String query)
{
SqlConnection con=new SqlConnection(connection);//connection from elsewhere
SqlCommand cmd = new SqlCommand(query);
cmd.connection=con;
con.Open();
SqlDataReader result=cmd.ExecuteReader();
//How to convert result to a list when i do not know the table structure
}
现在我想将结果转换为列表。 但是我不知道查询所处理的表的详细信息。查询可以有所不同,它可以查询任何表。 那么在这些条件下我怎样才能将结果转换为列表。是否可以?
这给我留下了一个简单的问题,如果我不知道表的详细信息,那么列表的条目将是什么,或者它将是什么的列表?
为了回答这个问题,我问自己另一个问题,是否可以有一个列表,其中每个列表条目都对应于 SqlDataReader 结果的一行?
所以我可以使用 List <数据行> x =result.Select().ToList(),但我也不想使用这个。
我希望列表中的每个条目映射到结果的每一行,同时由构成一行的原子数据类型组成。
例如从学生中选择studentname,studentid; 这将返回一个 SqlDataReader 结果,我想构造一个类型包含字符串和 int 的列表,然后用结果的行填充该列表。
当查询更改为 selectmarks,subject,grade frommarkswherestudentid=1432 时,我想构造一个类型包含 int,string,char 的列表,然后用该列表的行填充该列表结果。
有办法做到吗?
PS 如果我返回上面详细说明的列表,用户知道如何选择值,因为他创建了查询并且知道需要多少列。
Friends,
I have a dynamic sql query that I would like to execute and return a list.
From most of my internet searches on goole, I found that the type of the list has to be known to convert the sqldatareader to a list.
How can I do that when I do not know what query will be executed.
Execute(String query)
{
SqlConnection con=new SqlConnection(connection);//connection from elsewhere
SqlCommand cmd = new SqlCommand(query);
cmd.connection=con;
con.Open();
SqlDataReader result=cmd.ExecuteReader();
//How to convert result to a list when i do not know the table structure
}
Now I want to convert the result to a list.
However I do not know the details of the table which the query works on.The query can vary, it can query any table.
So under these conditions how can I convert the result to a list. Is it possible?
This leaves me with one simple question, if I do not know the table details, then what will be the entries of the list or it will be a list of what?
To answer that question, I ask myself another question, is it possible to have a list where each list entry will correspond to one row of the SqlDataReader result?
So I can use a List < DataRow > x =result.Select().ToList(), but I do not want to use this either.
I want each entry of the list to map to each row of the result and at the same time be composed of the atomic datatypes which make a row.
Eg Select studentname,studentid from student;
This will return me a SqlDataReader result and I want to construct a list whose type contains a string and an int and then populate the list with the rows of the result.
When the query changes to select marks,subject,grade from marks where studentid=1432, then I want to construct a list whose type contains int,string,char and then fill the list with the rows of the result.
Is there a way to do it?
PS The user knows how to pick the values if I return a list as detailed above, since he created the query and he knows how many columns to expect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果调用者知道类型,则 make 是一个通用方法,并每行创建一个
T
...或者,因为 dapper-dot-net 已经这样做了:
如果调用者也不知道 T,您可以使用
ExpandoObject
填充List
,将 Expando 转换为IDictionary< /代码>用键/值对填充它。
或者,由于 dapper-dot-net 已经这样做了:
那么调用者可以使用:
If the caller will know the type, make is a generic method, and create a
T
per row...Or, since dapper-dot-net already does that:
If the caller can't know the T either, you could use
ExpandoObject
to populate aList<dynamic>
, casting the expando toIDictionary<string,object>
to populate it with key/value pairs.Or, since dapper-dot-net already does that:
Then the caller can use: