以下是左轻候在Rich Explorer中的一段代码
通过调用ConfigConnstr就可能了,剩下的代码你自已可
以再精简一下
unit dm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, grids, oledb, ActiveX, comobj, olectrls, ADODB_TLB;
type
TCursorLocation = (ad_UseServer, ad_UseClient);
TParameterDirection = (ad_ParamUnknown, ad_ParamInput, ad_ParamOutput, ad_ParamInputOutput,
ad_ParamReturnValue);
TDM1 = class(TDataModule)
procedure DataModuleCreate(Sender: TObject);
private
{ Private declarations }
FQuesCount: integer;
ADOConnection1: _Connection;
DSLocal: _Recordset;
DSQues: _Recordset;
DSEdit: _Recordset;
QlistPos: integer;
ShowData: boolean;
procedure DSEditQuery(ssql: string);
procedure SaveConnStr(strConn: string);
function LoadConnStr: string;
public
{ Public declarations }
ConnStrFile: string;
sgQList: TStringGrid;
function PromptDataSource(ParentHandle: THandle; InitialString: WideString): WideString;
procedure SelectNoRoom;
function ConfigConnstr: string;
property QuesCount: integer read FQuesCount;
end;
var
DM1: TDM1;
EmptyParam: OleVariant;
const
CursorLocationValues: array[TCursorLocation] of TOleEnum = (adUseServer, adUseClient);
implementation
{$R *.DFM}
{ TDM1 }
procedure TDM1.DataModuleCreate(Sender: TObject);
begin
TVarData(EmptyParam).VType := varError;
TVarData(EmptyParam).VError := $80020004;
ShowData := false;
ADOConnection1 := CreateOleObject('ADODB.Connection') as _Connection;
DSLocal := CreateOleObject('ADODB.Recordset') as _Recordset;
DSQues := CreateOleObject('ADODB.Recordset') as _Recordset;
DSLocal.CursorType := CursorLocationValues[ad_UseServer];
DSEdit := CreateOleObject('ADODB.Recordset') as _Recordset;
end;
procedure TDM1.SelectNoRoom;
begin
// if DSLocal.Active = true then DSLocal.Close;
// ShowData := false;
end;
procedure TDM1.SaveConnStr(strConn: string);
var
sltConn: TStringlist;
begin
sltConn := TStringList.Create;
sltConn.Text := strConn;
sltConn.SaveToFile(ConnStrFile);
sltConn.free;
end;
function TDM1.LoadConnStr: string;
var
sltConn: TStringlist;
begin
sltConn := TStringList.Create;
if FileExists(ConnStrFile) then
sltConn.LoadFromFile(ConnStrFile)
else
sltConn.Text := '';
Result := sltConn.Text;
sltConn.free;
end;
procedure TDM1.DSEditQuery(ssql: string);
var
Source: OleVariant;
begin
with DSEdit do
begin
if state <> AdStateClosed then close;
Source := ssql;
Open(Source, ADOConnection1, AdOpenForwardOnly, AdLockReadOnly, adCmdText);
end;
end;
function TDM1.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;
function TDM1.ConfigConnstr: string;
begin
Result := PromptDataSource(Application.Handle, ADOConnection1.ConnectionString);
end;
end.