在Delphi中调用"数据链接属性"对话框设置ConnectionString
项目需要使用"数据链接属性"对话框来设置ConnectionString,查阅了一些资料,解决办法如下:
1.Delphi
在Delphi中比较简单,步骤如下:
方法1: use ADOConED;
ADOConED.EditConnectionString(ADOConnection1);
这样我们就可以使用:
ShowMessage(ADOConnection1.ConnectionString)
查看ADOConnection1的ConnectionString了
方法2: ADOConnection1.ConnectionString:=PromptDataSource(handle,'');
或者:
ADOConnection1.ConnectionString:=PromptDataSource(handle,ADOConnection1.ConnectionString);
附:PromptDataSource函数的代码:
function PromptDataSource(ParentHandle: THandle; InitialString: WideString): WideString;
var
DataInit: IDataInitialize;
DBPrompt: IDBPromptInitialize;
DataSource: IUnknown;
InitStr: PWideChar;
begin
Result := InitialString;
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;
if Succeeded(DBPrompt.PromptDataSource(nil, ParentHandle,
DBPROMPTOPTIONS_PROPERTYSHEET, 0, nil, nil, IUnknown, DataSource)) then
begin
InitStr := nil;
DataInit.GetInitializationString(DataSource, True, InitStr);
Result := InitStr;
end;
end;