【问题标题】:"No data exists for the row column" error returned by 'OleDbDataReader''OleDbDataReader' 返回的“行列不存在数据”错误
【发布时间】:2017-11-06 09:35:27
【问题描述】:

我正在尝试使用OleDbDataReader,但它返回此错误:

No data exists for the row column

我正在使用 Access 数据库。谁能帮我确定这个错误的来源?这是我的代码:

private void UpdateStudent_Load(object sender, EventArgs e)
{
    OleDbDataAdapter da = new OleDbDataAdapter();

    da.SelectCommand = new OleDbCommand(
        "select * from Students where SID= " + U_ID, con);

    con.Open();
    OleDbDataReader rd = da.SelectCommand.ExecuteReader();

    if (rd.Read())
    {
        //txtId.Text = U_ID;
        txtId.Text = rd["SID"].ToString();
    }

    rd.Close();
}

【问题讨论】:

  • 考虑实现using() 语句和try/catch。此外,您可能希望将代码更改为if(rd.HasRows) { while(rd.Read()) { do stuff } }。这些建议并没有解决您的实际问题

标签: c# oledbdatareader


【解决方案1】:

你可以试试这段代码

var queryString = "select * from Students where SID= " + U_ID, con;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    OleDbCommand command = new OleDbCommand(queryString, connection);
    connection.Open();
    OleDbDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        txtId.Text = reader["SID"].ToString();
    }
    reader.Close();
}

【讨论】:

  • 现在它没有给我错误 no "data exists for the row column"
  • 因为您的查询结果为空,如果您在数据库中尝试此查询,您的查询返回 null,我确定 user1587902
  • 但它会跳过 while 循环并立即转到 reader.close();
  • @user1587902 它的意思是 reader.Read() 在执行查询后没有读取任何数据。如果数据库中确实存在,您应该检查您的“U_ID”。
猜你喜欢
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多