我可以通过MySqlDataReader将再次复位和​​循环? [英] Can I reset and loop again through MySqlDataReader?

查看:328
本文介绍了我可以通过MySqlDataReader将再次复位和​​循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySqlDataReader将对象,有了这个查询的结果:

I have a MySqlDataReader object, with the result of this query :

SELECT仓库,租赁,运输,维修,人力零售WHERE区='中央'GROUP BY仓库

然后我遍历DataReader的一次,

And then I loop through the DataReader once,

while (r2.Read())
{
strXml.AppendFormat("<set label = '{0}'></set>",r2["warehouse"].ToString());
}

现在我通过它想再次循环... !!

And now I want to loop through it again...!!

我知道,DataReader的仅仅是一个只进的对象。但就是对我有任何其他的解决办法吗?

I know that DataReader is only a 'forward-only' object.. But is there any other solution for me here ?

我问,有没有持有多了MySqlDataReader其他数据的任何有效的方式?

I'm asking, is there any any efficient way to hold data other than MySqlDataReader ?

推荐答案

您可以使用如下:

using (MySqlConnection connMySql = new MySqlConnection(global.g_connString))
            {
               MySqlCommand cmd = connMySql.CreateCommand();
                cmd.CommandText = "selece * from <table>"; 
                connMySql.Open();
                using (MySqlDataReader dr = cmd.ExecuteReader())
                {
                        DataTable dt1 = new DataTable();
                        dt1.Load(dr);
                        // You can use this dt1 anywhere in the code
                 }

//解析数据表

// parsing datatable

 DataTable dt = new DataTable();
    if (dt.Rows.Count > 0)
    {
        for (int count = 0; count < dt.Rows.Count; count++)
        {
            string str= dt.Rows[count]["[ColumnName]"].ToString();  
        }
    }

这篇关于我可以通过MySqlDataReader将再次复位和​​循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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