How i insert a string from DataBase into a ListBox
string SQL = "SELECT * FROM Email ";
string myConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + textBox1.Text;
OleDbConnection myConnection = new OleDbConnection(myConnString);
OleDbCommand myCommand = new OleDbCommand(SQL, myConn开发者_如何学编程ection);
myConnection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
View.Items.Add(myReader.Read());
}
myConnection.Close();
The output is False .The output should be israel.nahum(from the DB)
You need to specify which column you want to retrieve the text from:
string SQL = "SELECT * FROM Email ";
string myConnString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + textBox1.Text;
OleDbConnection myConnection = new OleDbConnection(myConnString);
OleDbCommand myCommand = new OleDbCommand(SQL, myConnection);
myConnection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader();
//Changed if(myReader.Read()) to while(myReader.Read())
while(myReader.Read())
{
View.Items.Add(myReader["NameOfColumnGoesHere"]);
}
myConnection.Close();
Take a look at the documentation for the OleDbDataReader Class.
Read get you access to the iterator (put the next item of the results in it) use
if (myReader.Read())
{
View.Items.Add(myReader["COLUMN_NAME"]);
}
And so then, if the reader returns something then it'll put in on your item.
Please look in MSDN documentation how to use the DataReader object. the Read is reading a row and returning true if there is any row available, you should use other methods to get the values from the current row.
also, notice that you do not need to do a SELECT * FROM...
if you only need one column, just do SELECT Email FROM ...
for example...
精彩评论