public OleDbDataReader ExecuteQueryReturnReader(string sql, string connStr)
        {
            OleDbConnection conn = new OleDbConnection();
            OleDbDataReader reader = null;
            try
            {
                conn = new OleDbConnection(connStr);
                conn.Open();                OleDbCommand command = conn.CreateCommand();
                command.CommandText = sql;
                reader = command.ExecuteReader();
            }
            catch
            {
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
            return reader;
        }      
以上是在下的代码,在项目中是这么使用的: string DataBase = "HCConfig.mdb";
            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
                                    + Application.StartupPath + "//" + DataBase;
            ExecSql con = new ExecSql();
            string Sql= "select * form config where ID='sss'";
            OleDbDataReader p = con.ExecuteQueryReturnReader(strConnection, Sql);
            MessageBox.Show(p.FieldCount.ToString());但是为什么会出现以下的错误?
其他信息: 未将对象引用设置到对象的实例。
每次执行到 MessageBox.Show(p.FieldCount.ToString());的时候总会出现这句话!!!!

解决方案 »

  1.   

    OleDbDataReader p = con.ExecuteQueryReturnReader(Sql,strConnection);//是不是传反了?
      

  2.   

     finally
                {
                    if (conn != null)
                        conn.Close();
                }
    连接已经在返回前关闭了.这个不用自己写,已经有成熟的sqlhelper或oledbhelper.
      

  3.   

    去掉 if (conn != null)
                        conn.Close();也不行我试过的
      

  4.   

    如果说楼上2为说的错误你修改好了还是报错,那么我只能想到的是你的数据库查询的问题了!你确定你执行SQL返回的OleDbDataReader的实例P不为null?
      

  5.   

    好吧,刚回复一看你的SQL语句,我现在很肯定是你的OleDbDataReader的实例P为null了,你的SQL语句错误:select * form config where ID='sss' 中的form写错了!是from!!!导致P为null,那么你后面的获取其属性值肯定是未将对象引用到对象的实例了!