even if 表table中没有数据时, Read shouldn't cause errors, try to see what the error isOleDbDataReader areader = null;try
{
OleDbCommand comm = new OleDbCommand("select * from table",connect);
areader = comm.ExecuteReader();
areader.Read();
}
catch(OleDbException ep)
{
MessageBox.Show(ep.Message);
return;
}
finally
{
if (areader != null)
areader.Close();
}
{
OleDbCommand comm = new OleDbCommand("select * from table",connect);
areader = comm.ExecuteReader();
areader.Read();
}
catch(OleDbException ep)
{
MessageBox.Show(ep.Message);
return;
}
finally
{
if (areader != null)
areader.Close();
}
定义constr为全局变量,内容为连接数据库的字符串。
然后:
OleDbConnection cn=new OleDbConnection(constr)
OleDbCommand comm = new OleDbCommand("select * from table",cn);
cn.Open();//打开连接
OleDbDataReader areader = comm.ExecuteReader();
if(areader.Read())
{
//有记录
}
else
{
//没有记录
}
cn.Close();//关闭连接