Can I reset and loop again through MySqlDataReader?

故事扮演 提交于 2019-12-08 05:50:41

问题


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

SELECT warehouse, leasing, transportation, maintenance, manpower FROM retail WHERE zone = 'Central' GROUP BY warehouse

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...!!

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

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


回答1:


You can use below :

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();  
        }
    }


来源:https://stackoverflow.com/questions/6580065/can-i-reset-and-loop-again-through-mysqldatareader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!