//Open connection to Access database.
oleConn.Open();
//Query for searching the same user name exists in the database.
String sqlquery = "SELECT SO.sub_code, S.session, S.year FROM Subject_Offer AS SO, Sessions AS S WHERE SO.user_name='jm637' And SO.sess_id=S.sess_id";
//String sqlquery = "SELECT sub_code FROM Subject_Offer WHERE user_name='" + Logname + "'";
MessageBox.Show(sqlquery);
OleDbCommand command = new OleDbCommand(sqlquery, oleConn);
OleDbDataReader reader = command.ExecuteReader();
//If the record of that user found, list
if (reader.Read())
{
//load the table record of the week found.
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlquery, oleConn);
myDataSet = new DataSet();
adapter.Fill(myDataSet, "SubjectList");
//Display the table in Data Grid View.
dataGridView.DataSource = myDataSet.Tables["SubjectList"].DefaultView;
}
reader.Close();
oleConn.Close();sql命令我在access里试过,没有错误,数据库连接也对,但是每次reader这里就出错了。求解决
oleConn.Open();
//Query for searching the same user name exists in the database.
String sqlquery = "SELECT SO.sub_code, S.session, S.year FROM Subject_Offer AS SO, Sessions AS S WHERE SO.user_name='jm637' And SO.sess_id=S.sess_id";
//String sqlquery = "SELECT sub_code FROM Subject_Offer WHERE user_name='" + Logname + "'";
MessageBox.Show(sqlquery);
OleDbCommand command = new OleDbCommand(sqlquery, oleConn);
OleDbDataReader reader = command.ExecuteReader();
//If the record of that user found, list
if (reader.Read())
{
//load the table record of the week found.
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlquery, oleConn);
myDataSet = new DataSet();
adapter.Fill(myDataSet, "SubjectList");
//Display the table in Data Grid View.
dataGridView.DataSource = myDataSet.Tables["SubjectList"].DefaultView;
}
reader.Close();
oleConn.Close();sql命令我在access里试过,没有错误,数据库连接也对,但是每次reader这里就出错了。求解决
{
//load the table record of the week found.
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlquery, oleConn);
myDataSet = new DataSet();
在你的上面的//load the table record of the week found.下面加上一行,reader.Close();试试
{
oleConn.Open();
//Query for searching the same user name exists in the database.
String sqlquery = "SELECT SO.sub_code, S.session, S.year FROM Subject_Offer AS SO, Sessions AS S WHERE SO.user_name='jm637' And SO.sess_id=S.sess_id";
//String sqlquery = "SELECT sub_code FROM Subject_Offer WHERE user_name='" + Logname + "'";
MessageBox.Show(sqlquery);
OleDbCommand command = new OleDbCommand(sqlquery, oleConn);
OleDbDataReader reader = command.ExecuteReader();
//If the record of that user found, list
if (reader.Read())
{
//load the table record of the week found.
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlquery, oleConn);
myDataSet = new DataSet();
adapter.Fill(myDataSet, "SubjectList");
//Display the table in Data Grid View.
dataGridView.DataSource = myDataSet.Tables["SubjectList"].DefaultView;
}}
catch(Exception err)
{
MessageBox.Show(err.Message.ToString(),"提示");
}建议加个
异常处理,然后用MessageBox.show()显示出来。有很清楚的中文提示。以便排错
while(reader.Read())//这样一直读完为止
{
//用适配器填充数据的代码
}