"No data exists for the row column"错误由'OleDbDataReader'返回



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

No data exists for the row column

我正在使用访问数据库。任何人都可以帮助我确定此错误的来源吗?这是我的代码:

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();
}

你可以试试这段代码

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();
}

相关内容

  • 没有找到相关文章

最新更新