“OleDbDataReader”返回的“行列没有数据”错误

时间:2012-08-09 14:47:40

标签: c# oledbdatareader

我正在尝试使用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();
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码

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