从数据库中检索名称并使用csharp显示在列表框中 [英] Retrieving the name from database and displayed into teh listbox using csharp

查看:136
本文介绍了从数据库中检索名称并使用csharp显示在列表框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MS Access数据库。



MSAccess中的设计如下;



Faculty_Name Text





我的代码如下加载按钮;



当我单击要从数据库中检索并显示在列表框中的加载按钮faculty_Name。



  string  connstring =   Provider = Microsoft.Jet.OLEDB.4.0; Data Source = E:\\ \\\Projects \\Faculty Schedule \\Faculty Schedule \\Data\\HIMTTESTing.mdb;; 

OleDbConnection OleDbCon = new OleDbConnection(connstring);
OleDbCon.Open();
string sql1 = 从Tb_SCH_Faculty_Details中选择Faculty_Name ;
OleDbCommand cmd = new OleDbCommand(sql1,OleDbCon);
OleDbDataReader reader = cmd.ExecuteReader();
{
Lb_Faculty_Name.Items.Add(读者);
}
OleDbCon.Close();



当我在列表框中运行时,Faculty_Name没有显示。



来自我上面的代码是什么我可以这么做。请帮助我。



Jibesh添加的代码块[/ Edit]

解决方案

你必须逐个添加记录

 while(reader.Read())
{
Lb_Faculty_Name.Items.Add(reader [0] .tostring());
}


你必须使用循环语句来读取阅读器并将数据添加到列表框即



while(reader.Read())

{

Lb_Faculty_Name.Items.Add(reader.GetString(0));

$





其中reader.GetString()

得到包含所有列的行' 'reader.GetString(0)''获取当前行的第一个列值

I am using MS Access Database.

Design in MSAccess as follows;

Faculty_Name Text


My Code as follows in Load button;

when i click the Load button faculty_Name to be retrieved from the DB and displayed in the List box.

string connstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= E:\\Projects\\Faculty Schedule\\Faculty Schedule\\Data\\HIMTTESTing.mdb;";

OleDbConnection OleDbCon = new OleDbConnection(connstring);
OleDbCon.Open();
string sql1 = "select Faculty_Name from  Tb_SCH_Faculty_Details ";
OleDbCommand cmd = new OleDbCommand(sql1, OleDbCon);
OleDbDataReader reader = cmd.ExecuteReader();
{
   Lb_Faculty_Name.Items.Add(reader);
}
OleDbCon.Close(); 


when i run in the List box the Faculty_Name is not displaying.

from my above code what is the mistake.how can i do.please help me.

[Edit]Code block added by Jibesh[/Edit]

解决方案

you have to add one by one records

while (reader.Read())
{
Lb_Faculty_Name.Items.Add(reader[0].tostring());
}


you have to use loop statement to read the reader and add data to listbox i.e

while (reader.Read())
{
Lb_Faculty_Name.Items.Add(reader.GetString(0));
}


where reader.GetString()
get the row with all colums as given ''reader.GetString(0)'' gets the first colum value of the current row


这篇关于从数据库中检索名称并使用csharp显示在列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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