Delphi ODBC Connection Dialog Component?

谁说胖子不能爱 提交于 2019-11-28 01:34:47

问题


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


回答1:


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.




回答2:


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;



来源:https://stackoverflow.com/questions/1476772/delphi-odbc-connection-dialog-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!