SqlDataReader对象 - 如何当前行转换为字典 [英] SqlDataReader - How to convert the current row to a dictionary

查看:141
本文介绍了SqlDataReader对象 - 如何当前行转换为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法来一个SqlDataReader的当前行的所有列转换为字典?

Is there an easy way to convert all the columns of the current row of a SqlDataReader to a dictionary?

using (SqlDataReader opReader = command.ExecuteReader())
{
// Convert the current row to a dictionary
}

感谢

推荐答案

比这更简单:

// Need to read the row in, usually in a while ( opReader.Read ) {} loop...
opReader.Read();

// Convert current row into a dictionary
Dictionary<string, object> dict = new Dictionary<string, object>();
for( int lp = 0 ; lp < opReader.FieldCount ; lp++ ) {
    dict.Add(opReader.GetName(lp), opReader.GetValue(lp));
}



我仍然不知道你为什么会需要从一种类型的这一特定的转换收集到另一个地方。

I'm still not sure why you would need this particular transformation from one type of collection to another.

这篇关于SqlDataReader对象 - 如何当前行转换为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆