C#winform怎么读取数据库信息显示出来

oCream1 2012-10-20 10:42:51
我用了list view控件了
但是不知道代码有没有错 请告诉纠正一下或者详细教我下 只显示数据库3条信息而已 nvchar date money


private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
string connStr1 = "Data Source=.;Initial Catalog=Plane;Integrated Security=True";
SqlConnection conn1 = new SqlConnection(connStr1);
conn1.Open();



string sql1 = "select * from plane ";
SqlDataReader reader1 = null;
SqlCommand cmd1 = new SqlCommand(sql1, conn1);
reader1 = cmd1.ExecuteReader();
while (reader1.Read() )
{
string city=reader1["city"].ToString();
string time=(reader1["time"] + "\t");
string money=(reader1["money"] + "\t");



}
reader1.Close();
...全文
1143 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pfei_cs 2014-07-24
  • 打赏
  • 举报
回复
可以回复了没有呀
小枪 2012-10-21
  • 打赏
  • 举报
回复
首先 事件选择失误 当选项编号发生改变激发事件 你还未将值赋予控件 怎么会有选项 怎么会触发此事件
其次 没有吧拿到数据库里的值付给窗体上的listview控件
将每轮循环拿到的值付给一个listviewItem对象 吧这个对象加入你的Listview控件中
wuyq11 2012-10-21
  • 打赏
  • 举报
回复
ile (reader.Read())
{ //读取DataReader中的数据并输出
ListViewItem ddd = new ListViewItem();
string city=reader1["city"].ToString();
string time=reader1["time"] + "\t";
string money=reader1["money"] + "\t";
ddd.SubItems[0].Text =city+time+money;
listView1.Items.Add(ddd);
}
java_xiao_ye 2012-10-20
  • 打赏
  • 举报
回复
把上面的赋值变量添加到我用双引号里
ListViewItem item = new ListViewItem("这里把要显示的首项加上!");
item.SubItems.AddRange(new string[] { "第二项", "第三项", "等等。。。" });
ListView控件名.items.add(item);把上面填加的项放到控件集合中
dalmeeme 2012-10-20
  • 打赏
  • 举报
回复
双击窗体,写在Form1_Load方法中:

把while循环里的代码改成:
while (reader1.Read() )
{
string city=reader1["city"].ToString();
string time=(reader1["time"] + "\t");
string money=(reader1["money"] + "\t");
ListView1.Items.Add(city+time+money);


}
oCream1 2012-10-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
你想把数据显示在哪里?
[/Quote]显示在窗体啊
王子文龙 2012-10-20
  • 打赏
  • 举报
回复
你想把数据显示在哪里?
yifan_huang 2012-10-20
  • 打赏
  • 举报
回复
在窗体初始化里面绑定就行了,就是form_load事件里
a278612114 2012-10-20
  • 打赏
  • 举报
回复
大概用错了事件,

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧