public static DataSet GetDataSet(string sql, string tableName)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    OleDbDataAdapter command = new OleDbDataAdapter(sql, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.OleDb.OleDbException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }
在access里面执行语句正常,但是就是查询的时候报错
不知如何是好啊,在线等待

解决方案 »

  1.   

                    catch (System.Data.OleDb.OleDbException ex)
                    {
                        throw new Exception(ex.Message);
                    }
    这个画蛇添足了,应给写为
                    catch (System.Data.OleDb.OleDbException )
                    {
                        throw;
                    }
      

  2.   


    当调用函数,运行到command.Fill(ds, "ds");语句时就报错
      错误如下:
      捕捉到 System.AccessViolationException
      Message="尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"
      Source="System.Data"
      StackTrace:
      在 System.Data.Common.UnsafeNativeMethods.ICommandText.Execute(IntPtr pUnkOuter, Guid& riid, tagDBPARAMS pDBParams, IntPtr& pcRowsAffected, Object& ppRowset)
      在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForMultpleResults(tagDBPARAMS dbParams, Object& executeResult)
      在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
      在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
      在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
      在 System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
      在 System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
      在 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
      在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
      在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
      

  3.   

    你把OleDbDataAdapter 的引用删除掉再引用看一下在第一个框里面引用 
      

  4.   

    这个问题多半是oledb层出错,建议重装oledb,或者重装操作系统,也可以在其他机器上测试有没有类似问题,
      

  5.   

     System.AccessViolationException
    在试图读写受保护内存时引发的异常。把其他程序关掉看看,不行注销下