如何使用SqlDataReader填充Listview [英] How Can I Populate Listview With SqlDataReader

查看:271
本文介绍了如何使用SqlDataReader填充Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,亲爱的朋友,请告诉我如何将SqlDataReader数据添加到Listview的数据源中.我正在使用DataSet进行此操作,但是我想用SqlDataReader填充它,因为这将是从数据库中获取数据的最快方法.所以,请尽快给我答案...............

Hello Dear Friends Please tell me the way how can i add SqlDataReader Data to Listview''s DataSource. I am doing it with DataSet but I want to populate it with SqlDataReader bacause it will be the fastest way to fetch data from Database. So please give me Answer soon..............

推荐答案

您可以引用 ^ ]


请参见
http://social.msdn.microsoft.com /Forums/zh-CN/winforms/thread/4b7eb940-feb4-474c-99cd-ebaf6998620b/ [ http://forums.asp.net/t/1334425.aspx [
See
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/4b7eb940-feb4-474c-99cd-ebaf6998620b/[^]
http://forums.asp.net/t/1334425.aspx[^]


SqlCommand cmd = new SqlCommand("commandText,con"); // here commandtext is your query or name of the procedure that will return the Rows and con is a SqlConnection object
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
  ListViewItem obj=new ListViewItem(Convert.ToString(dr[0]),Convert.ToString(dr[1]);
  //in object of ListViewItem give display member at first and give value member at second position 
  listView1.Items.Add(obj); // add object to the listbox
}
con.Close();


这篇关于如何使用SqlDataReader填充Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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