一。udl文件关联程序是OLE DB CORE SERVICE,所以双击会弹出DATA LINK PROPERTIES对话框,可以有记事本打开,
记录了数据库联接的相关信息。 IDataInitialize它是OLEDB单元提供的一个接口, 通过它可以创建读写数据库连接的初始信息到指定的UDL文件。
示例:
implementation
uses oledb,ActiveX,ComObj; //引入单元
{ $R *.dfm }
{ TForm1 }
procedure TForm1.Btn1Click(Sender: TObject);
var
fileName,serName:String;
begin
fileName: = ExtractFilepath((Application.ExeName)) + ' sql.UDL ' ;
Init_ConnectionString(fileName,serName);
mmo1.Lines.Add(serName);
// serName为:Provider = SQLOLEDB. 1 ;Integrated Security = SSPI;Persist Security Info = False;
// Initial Catalog = Northwind;Data Source = 34F6596280EA4B5\sql2000
uses oledb,ActiveX,ComObj; //引入单元
{ $R *.dfm }
{ TForm1 }
procedure TForm1.Btn1Click(Sender: TObject);
var
fileName,serName:String;
begin
fileName: = ExtractFilepath((Application.ExeName)) + ' sql.UDL ' ;
Init_ConnectionString(fileName,serName);
mmo1.Lines.Add(serName);
// serName为:Provider = SQLOLEDB. 1 ;Integrated Security = SSPI;Persist Security Info = False;
// Initial Catalog = Northwind;Data Source = 34F6596280EA4B5\sql2000
try
Con1.Open;
mmo1.Lines.Add(BoolToStr(Con1.Connected)); //-1
except
mmo1.Lines.Add('异常了');
mmo1.Lines.Add(BoolToStr(Con1.Connected)); //0
end;
Con1.Open;
mmo1.Lines.Add(BoolToStr(Con1.Connected)); //-1
except
mmo1.Lines.Add('异常了');
mmo1.Lines.Add(BoolToStr(Con1.Connected)); //0
end;
end ;
procedure TForm1.Init_ConnectionString(FileName: string ; var ConString: string ); //ConString用了传址方式
var
DataInit:IDataInitialize; // 定义接口
fname,s:POleStr;
tmp:widestring;
begin
tmp: = filename; //注意必须经过widString转化,否则得不到值
fname: = POleStr(tmp);
DataInit: = CreateComobject(CLSID_DATALINKS) as IDataInitialize; // 创建接口
DataInit.LoadStringFromStorage(fname,s);
mmo1.Lines.Add(s);
ConString: = s;
DataInit: = nil ; // 释放
end ;
end .