试着学习数据库,首先建一窗体,再建一DATA MODULE,名称database,里面有三个控件,adoq1(Tadoquery),datas1(Tdatasource),adoc1(Tadoconnect),这三个控件什么也没有设定,只是更改了name属性.
代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
  with database do
  begin
  adoq1.Connection :=adoc1;
  adoq1.SQL.Clear ;
  adoq1.SQL.Add('select * from khzl');
  adoq1.Active :=true;
  datas1.DataSet:=adoq1;
  dbgrid1.DataSource:=datas1;
  end;
end;出现以下错误:
Debugger   Exception   Notification 
--------------------------- 
Project   Project1.exe   raised   exception   class   EAccessViolation   with   message   'Access   violation   at   address   0048747F   in   module   'Project1.exe '.   Read   of   address   0000005C '.   Process   stopped.   Use   Step   or   Run   to   continue. 
请问这是为什么??
我的理解是没有指定数据库名称而出现错误,但是别人的代码里也是这样写的能编译运行.这是为什么,在线等!

解决方案 »

  1.   

    可能是找不到数据库(access的mdb文件)而引发的地址访问异常。
    在你的IDE环境下,如果TADOConnection的连接串设置了,并且connected设置为true,在其他机器环境下,可能就引发异常,比如由于你检索数据库文件采用绝对路径,造成数据文件找不到等。
      

  2.   

    database要在Form1运行之前运行,否则不能与数据库建立连接,可以在OPTION-FORM中将database移到其他窗体之前。
      

  3.   

    原来在database的create事件有数据库的连接
    procedure Tdatabase.DataModuleCreate(Sender: TObject);
    begin
     adoc1.ConnectionString :='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+extractfilepath(application.exename)+'data\jxc.mdb;Persist Security Info=False';
    end;
    为什么我加上后还是报错!错误信息如下:
    Build
      [Error] data_unit.pas(29): Undeclared identifier: 'application'
      [Error] data_unit.pas(29): ')' expected but identifier 'exename' found
      [Fatal Error] test.dpr(6): Could not compile used unit 'data_unit.pas'
    这是什么?????望高手指教!
      

  4.   

    application.exename? 用ParamStr(0)就Ok啦
      

  5.   

    改用
      adoc1.ConnectionString   := 'Provider=Microsoft.Jet.OLEDB.4.0;Data   Source= '+extractfilepath(Paramstr(0))+ 'data\jxc.mdb;Persist   Security   Info=False '; 
    这里倒没有报错!但是到这里报错:
      adoq1.Connection :=adoc1;
    错误信息
    出现以下错误: 
    Debugger       Exception       Notification   
    ---------------------------   
    Project       Project1.exe       raised       exception       class       EAccessViolation       with       message       'Access       violation       at       address       0048747F       in       module       'Project1.exe   '.       Read       of       address       0000005C   '.       Process       stopped.       Use       Step       or       Run       to       continue.  
      

  6.   

    哥们你是不是动态创建了数据库窗体了
    在项目文件里改一下改成以下那样就好了,我也碰到过:
           FDM:=TDM.Create(application);