我是动态生成ADOConnection,但执行程序时,老出现尚未调用CoInitialize错误。

解决方案 »

  1.   

      ADOConnectionUserDatabase:=TADOConnection.Create(self);
      with ADOConnectionUserDatabase do
      begin
        //Connected:=True;
        ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog='+strUserDB+';Data Source=(local);';
        KeepConnection := False;
        LoginPrompt := False;
        Mode := cmReadWrite;
        Provider := 'SQLOLEDB.1';
        Open;
      end;
     
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Db, DBTables, Grids, DBGrids, ComCtrls, ADODB;type
      TForm1 = class(TForm)
        Button1: TButton;
        ADOConnection1: TADOConnection;
        procedure Button1Click(Sender: TObject);
      private
        
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    var
      tempCon: TADOConnection;
      tempStr: string;
    begin
      tempStr := 'xxxx'; //此处加入数据库连接字符串
      tempCon := TADOConnection.Create(Self);
      with tempCon do
      begin
        Connected := false;
        ConnectionString := '';
        ConnectionString := tempStr;
        try
          Connected := true;
          ShowMessage('Succeed!');
        except
          ShowMessage('Failed!');
          Exit;
        end;
      end;
      //.... 其它处理过程
    end;end.
      

  3.   

    大家看看我的代码:
    ADOConnectionSystemDatabase 是在private中定义的。strConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog='+strSystemDB+';Data Source=(local);';
      with ADOConnectionSystemDatabase do
      begin
        Connected:=False;
        ConnectionString:='';
        ConnectionString:=strConnectionString;
        KeepConnection := False;
        LoginPrompt := False;
        Mode := cmRead;
        Provider := 'SQLOLEDB.1';
        try
          Connected:=True;
        Except
          LogMessage('无法联接到数据库'+strSystemDB+'。',EVENTLOG_ERROR_TYPE);
        end;
      end;
    不知道还有什么问题。总是老样子的错误提示。