代码:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,inifiles, Grids, DBGrids;type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    DBGrid1: TDBGrid;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementationuses Unit3, Unit2;
 var myinifile:Tinifile;
{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var
filename,UID,DD,DS,ConStr:string;
begin
filename:=ExtractFilePath(paramstr(0))+'sqlini.ini';
  myinifile:=TInifile.Create(filename);
  UID:=myinifile.readstring('database','User ID','');
    DD:=myinifile.readstring('database','Initial Catalog','');
    Ds:=myinifile.readstring('database','Data Source','');
    ConStr:='Provider=SQLOLEDB.1;Persist Security Info=False;'+'User ID='+UID+';Initial Catalog='+DD+';Data Source='+DS; dm.adoconnection1.Connected:=false;///到这无法执行  try
  dm.adoconnection1.ConnectionString:=ConStr;
 dm.adoconnection1.Connected:=true;
   except
  application.MessageBox('费了!','连接失败',mb_ok);
   end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
form2.show;
end;
 
错误:access violation at addreaa 004808EA in module 'project1.exe'.read of address 00000058
ini文件[database]
User ID=sa;
Initial Catalog=gctj;
Data Source=yth
请诸位帮帮忙`要是有例子最好```[email protected]

解决方案 »

  1.   

    目标是要在form1的dbgrid显示表里的数据
    dm窗体里有adoconnection adotable datasource
      

  2.   

    connectionstring类似这样:
    adoconnection1.connectionstring:='Provider=SQLOLEDB.1;Password=preprep;Persist Security Info=True;User ID=wwwuser;Initial Catalog=custom;Data Source=139.139.10.54';
    你把你取的结果ShowMessage出来看看
    另外,你先赋值connectionstring再置 dm.adoconnection1.Connected := False;
    或者改成
    if dm.adoconnection1.Connected then  dm.adoconnection1.Connected := False;
      

  3.   

    try
        lpStrAppReg := GetRegistryValue('MachineName');
        iResult := TDES(0, lpStrAppReg, MachineName);    lpStrAppReg := GetRegistryValue('DBName');
        iResult := TDES(0, lpStrAppReg, DBName);    lpStrAppReg := GetRegistryValue('DBUser');
        iResult := TDES(0, lpStrAppReg, DBUser);    lpStrAppReg := GetRegistryValue('DBPassword');
        iResult := TDES(0, lpStrAppReg, DBPassword);
      except
        Application.MessageBox('请重新安装系统。','系统错误',MB_OK);
        Application.Destroy;
      end;  try
        with ADOCSQL do
        begin
          Connected := False;
          LoginPrompt := False;//True;
          ConnectionString := 'Provider=SQLOLEDB.1;Password='+DBPassword;
          ConnectionString := ConnectionString+';Persist Security Info=True;User ID='+DBUser;
          ConnectionString := ConnectionString+';Initial Catalog='+DBName;
          ConnectionString := ConnectionString+';Data Source='+MachineName;
          ConnectionString := ConnectionString+';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=ZKWU;Use Encryption for Data=False;Tag with column collation when possible=False';
          DefaultDatabase := DBName;
          Connected := True;
        end;
      except
        Application.MessageBox('请联系系统管理员进行网络设置!','连接错误',MB_ICONERROR+MB_OK);
        Application.Destroy;
      end;
      

  4.   

    上例中采用了自行开发的3DES算法对保存在注册表中的字符串进行解密,然后组成ConnectionString。
      

  5.   

    代码:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,inifiles, Grids, DBGrids;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        DBGrid1: TDBGrid;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit3, Unit2;
     var myinifile:Tinifile;
    {$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
    filename,UID,DD,DS,ConStr:string;
    begin
    filename:=ExtractFilePath(paramstr(0))+'sqlini.ini';
      myinifile:=TInifile.Create(filename);
      UID:=myinifile.readstring('database','User ID','');
        DD:=myinifile.readstring('database','Initial Catalog','');
        Ds:=myinifile.readstring('database','Data Source','');
        ConStr:='Provider=SQLOLEDB.1;Persist Security Info=False;'+'User ID='+UID+';Initial Catalog='+DD+';Data Source='+DS; dm.adoconnection1.clsoe;///到这无法执行  try
      dm.adoconnection1.ConnectionString:=ConStr;
     dm.adoconnection1.Connected:=true;
       except
      application.MessageBox('费了!','连接失败',mb_ok);
       end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    form2.show;
    end;
     
    错误:access violation at addreaa 004808EA in module 'project1.exe'.read of address 00000058
    ini文件[database]
    User ID=sa;
    Initial Catalog=gctj;
    Data Source=yth
    请诸位帮帮忙`要是有例子最好```[email protected]
      

  6.   

    dm.adoconnection1.Connected:=false;///到这无法执行
    如果報地地錯, 最大的原因是 dm 全局變量沒有賦值(99%的原因).試試  dm := txxx.Create(nil);
      

  7.   

    dm就是 data module 窗体 怎么create?
      

  8.   

    除了 bigben2008(ben)方法没试.目前还不灵
      

  9.   

    重做练习
    将adoconnect adotable datasource
    放在form1上运行通过
    why```````````````````````````
      

  10.   

    解决了````````窗体创建循序错了
    现创建data module在创建form1````````````````这100分``````````5555555555