Delphi ODBC Connection Dialog Component?

2019-01-25 17:15发布

I am thinking about adding ODBC database connectivity to an application.

The user will at runtime configure and select their database odbc connection.

Are there any components that will give me the required series of dialogs ?

Allowing the user to select the data source type, select drivers, browse already defined ODBC connections etc.

Cheers Sam

2条回答
聊天终结者
2楼-- · 2019-01-25 17:31

PromptDataSource Function from the ADODB unit. Two parameters are required:

  1. the calling form's handle
  2. a connection string . If you want no default connection string just pass an empty string as:

    var sConn : WideString; begin sConn := PromptDataSource(Form1.Handle, ''); end;

查看更多
放荡不羁爱自由
3楼-- · 2019-01-25 17:38

You can try this, if you are using ADO components.

Option 1

  Uses
    OleDB,
    ComObj,
    ActiveX;

    function Edit_ADO_ODBC_ConnectionString(ParentHandle: THandle; InitialString: WideString;out NewString: string): Boolean;
    var
      DataInit  : IDataInitialize;
      DBPrompt  : IDBPromptInitialize;
      DataSource: IUnknown;
      InitStr   : PWideChar;
    begin
      Result   := False;
      DataInit := CreateComObject(CLSID_DataLinks) as IDataInitialize;
      if InitialString <> '' then
      DataInit.GetDataSource(nil, CLSCTX_INPROC_SERVER, PWideChar(InitialString),IUnknown, DataSource);
      DBPrompt := CreateComObject(CLSID_DataLinks) as IDBPromptInitialize;

      {
      DBPROMPTOPTIONS_WIZARDSHEET = $1;
      DBPROMPTOPTIONS_PROPERTYSHEET = $2;
      DBPROMPTOPTIONS_BROWSEONLY = $8;
      DBPROMPTOPTIONS_DISABLE_PROVIDER_SELECTION = $10;
      }
      if Succeeded(DBPrompt.PromptDataSource(nil, ParentHandle,DBPROMPTOPTIONS_PROPERTYSHEET, 0, nil, nil, IUnknown, DataSource)) then
      begin
        InitStr   := nil;
        DataInit.GetInitializationString(DataSource, True, InitStr);
        NewString := InitStr;
        Result    := True;
      end;
    end;



Result:=Edit_ADO_ODBC_ConnectionString(0,OldConnectionString,NewString);

Option 2

Uses
ADODB;

PromptDataSource(Self.Handle, InitialString);

Option 3

Uses
ADODB,
AdoConEd;

procedure TMainForm.Button2Click(Sender: TObject);
Var
ADOConnection1 : TADOConnection;
begin
   ADOConnection1:=TADOConnection.Create(Self);
   EditConnectionString(ADOConnection1);
end;

You must Select "Microsoft OLE DB Provider for ODBC Drivers"

Bye.

查看更多
登录 后发表回答