c# winform程序,怎样把数据库数据用oleDbDataReader读出来.急啊,在线等
我用c# 做一个winform通信程序,想知道怎样把数据库数据用oleDbDataReader读出来.想读出来放到数组中通过mscomm32发出去,大家讨论一下看我的思路是不是正确?
我的程序代码如下:
private string FormatDataReader(OleDbDataReader datareader)
{
string readerString= " ";
while(datareader.Read())
{
readerString+= "line_name= "+datareader[ "line_name "].ToString()+ " ";
readerString+= " <br> ";
}
datareader.Close();
return(readerString);
}
//
private OleDbDataReader ExecuteReader()
{
OleDbCommand oleDbCommand1=new OleDbCommand( "SELECT pkid,line_name,switch,password from line ",oleDbConnection1);
oleDbConnection1.Open();
OleDbDataReader dr=oleDbCommand1.ExecuteReader(CommandBehavior.CloseConnection);
return(dr);
}
private void btn_Click(object sender, System.EventArgs e)
{
listBox1.Text=FormatDataReader(ExecuteReader());
}
现在的情况是,我单击按钮btn,listbox上根本没有数据(我在工具箱拖的oleDbDataAdapter入在窗体上的),请问问题出在哪里,谢谢了.
------解决方案--------------------没有看你的代码,随手给你写一个吧
DataSet DS = new DataSet();
string sqlstrr = string.Empty;
string readerString = string.Empty;
OleDbConnection _oc = new OleDbConnection();
_oc.ConnectionString = string.Empty;
OleDbDataAdapter _oda = new OleDbDataAdapter(sqlstrr, _oc);
_oda.Fill(DS, "TestTable ");
if (DS.Tables[0].Rows.Count != 0)
{
for (int i = 0; i < DS.Tables[0].Rows.Count; i++)
{
readerString += "line_name= " + DS.Tables[0].Rows[i][ "line_name "].ToString() + " ";
}
}
关键字串 自己赋值