Sql server 2008 初始化TSQLConnection驱动程序非GUI应用程序

Sql server 2008 初始化TSQLConnection驱动程序非GUI应用程序,sql-server-2008,delphi,database-connection,Sql Server 2008,Delphi,Database Connection,我试图在运行时在非GUI应用程序上创建TSQLConnection Uses Data.DB, Data.SqlExpr, Data.DBXMSSQL; ... procedure TFoo.InitializeDB; begin if NOT Assigned(SQLCon) then exit; SQLCon.Params.Clear; SQLCon.ConnectionName := 'ReportsCon'; SQLCon.DriverName := 'MSSQ

我试图在运行时在非GUI应用程序上创建
TSQLConnection

Uses Data.DB, Data.SqlExpr, Data.DBXMSSQL; 
...
procedure TFoo.InitializeDB;
begin
  if NOT Assigned(SQLCon) then exit;
  SQLCon.Params.Clear;
  SQLCon.ConnectionName := 'ReportsCon';
  SQLCon.DriverName     := 'MSSQL';
  SQLCon.LoadParamsFromIniFile('C:\ConfigTest\DBTest.ini');
  try
    SQLCon.Connected      := True;
  except
    on E: Exception Do
      LastErr := E.Message;
  end;
end;
我犯了这个错误

DBX Error:  Driver could not be properly initialized.  Client library may be missing, not installed properly, of the wrong version, or the driver may be missing from the system path.
出于测试目的,我创建了一个新的GUI项目,我在表单上用下面的
OnClick
事件删除了
TButton

Uses Data.DB, Data.SqlExpr, Data.DBXMSSQL;
...
procedure TForm12.btn1Click(Sender: TObject);
var
  SQLConnection: TSQLConnection;
begin
  SQLConnection := TSQLConnection.Create(nil);
  try
    SQLConnection.ConnectionName := 'ReportsCon';
    SQLConnection.DriverName     := 'MSSQL';
    SQLConnection.LoadParamsFromIniFile('C:\ConfigTest\DBTest.ini');
    try
      SQLConnection.Connected      := True;
    except
      on E: Exception Do
        ShowMessage(E.Message);
    end;
  finally
    SQLConnection.Free;
  end;
end;
代码按预期运行,没有错误。

EXE和
dbxmss.dll
都在同一个文件夹中

C:\Users\$Name\Documents\Embarcadero\Studio\Projects\Win32\Debug

我在这里遗漏了什么?

您可能需要调用
CoInitialize(nil)
在启动时初始化COM库,并使用
CoUnInitialize
@kobik完成。您完全正确,现在可以工作了。您可能需要调用
CoInitialize(nil)
在启动时初始化COM库,用
coninitialize
@kobik完成广告,你完全正确,现在它可以工作了。