简体   繁体   中英

'No data exists for the row/column.' Oledb Exception

connection.Open();
OleDbCommand command = new OleDbCommand("SELECT [Names] FROM Test", 
connection);

OleDbDataReader reader = command.ExecuteReader();
string result = reader.GetValue(0).ToString();
        MessageBox.Show(result);
        connection.Close();

Could anyone help? I'm getting 'No data exists for the row/column.' this error thrown

You are not calling Read Method

    OleDbDataReader reader = command.ExecuteReader();
    if(reader.Read())
    {
       string result = reader.GetValue(0).ToString();
       MessageBox.Show(result);
     }
    connection.Close();

This will just read the first row from the result.If you want all the rows then you will need to write some thing like this

    OleDbDataReader reader = command.ExecuteReader();        
    List<string> data = new List<string>();
    while(reader.Read())
    {
      data.Add(reader.GetValue(0).ToString());
    }        
    connection.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM