如果决策方可以的话,给你一个函数 ConnStr_BDE_FromADO_MSSQL (从一个函数库中分出来的依赖别的函数,虽然长了一点,凑伙还可以用),可以把 ADO 的数据连接字符串转换成BDE的连接字符串,放一个 Database 用这个转换的参数连接数据库,然后再连接决策方
我自己经常需要用这个在 ADO 和 BDE 数据连接控件之间转换,呵呵
uses Bde, DBTables, Registry;
function RightStr(S:string;Count:integer):string;
begin
//获取字符串右边起n个字符
RightStr:=Copy(s,Length(s)-Count+1,Count);
end;
function RDelete(S,Sub:string):string;
begin
//删除字符串右边的某一所有字符
sub:=copy(sub,1,1);
while RightStr(S,1)=subdo
Delete(S,length(s),1);
RDelete:=s;
end;
function ConnStr_ADO_GetKeyVal(ADOConnStr:WideString;
Key:string):string;
var pIndex,c:integer;
vConnStr,kConnStr:string;
begin
//获取ADO连接字符串键值
Key:=RDelete(Trim(Key),';');
if Key='' then
exit;
vConnStr:=Trim(ADOConnStr);
kConnstr:=vConnStr;
pIndex:=pos(UpperCase(Key),UpperCase(vConnStr));
if pIndex>0
then
begin
Delete(vConnstr,1,pIndex-1);
c:=pIndex;
pIndex:=pos('=',vConnStr);
c:=c+pIndex-1;
Delete(vConnStr,1,pIndex);
pIndex:=pos(';',vConnStr);
if pIndex>0 then
vConnStr:=copy(vConnStr,1,pIndex-1);
end
else
vConnStr:='';
Result:=vConnStr;
end;
procedure ReturnDefaultConnStr(pAliasName:string;pDriverName:string;pDefaultConnStr:TStrings);
var AddPassword: Boolean;
fDataBase:TDatabase;
begin
pDefaultConnStr.Clear;
AddPassword:=False;
fDataBase:=TDatabase.Create(nil);
try
if pDriverName<>'' then
begin
fDatabase.Session.GetDriverParams(pDriverName,pDefaultConnStr);
AddPassword:=(pDriverName<> szCFGDBSTANDARD);
end else
if pAliasName<>'' then
begin
fDatabase.Session.GetAliasParams(pAliasName,pDefaultConnStr);
AddPassword:=fDatabase.Session.GetAliasDriverName(pAliasName)<>szCFGDBSTANDARD;
end;
if AddPassword then
pDefaultConnStr.Add('PASSWORD=');
finally
fDataBase.Free;
fDataBase:=nil;
end;
end;
function Create_ODBC_DSN_MSSQL(DSN,Server,Database:string;
TrustConn:boolean):boolean;
var RegTemp:TRegistry;
Driver:string;
begin
//建立MSSQL ODBC 数据源
Result:=true;
RegTemp:=TRegistry.Create;
with RegTempdo
try
RootKey:=HKEY_LOCAL_MACHINE;
//读取SQL Server驱动路径
if KeyExists('Software/ODBC/ODBCINST.INI/SQL Server') then
begin
if OpenKey('Software/ODBC/ODBCINST.INI/SQL Server',false) then
Driver:=ReadString('Driver')
else
begin
Result:=false;
Raise Exception.Create('无法获取MSSQL驱动信息,建立数据源失败!');
end;
CloseKey;
end
else
begin
Result:=false;
Raise Exception.Create('找不到MSSQL驱动,建立数据源失败!');
end;
if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources',True) then
begin
//注册一个DSN名称
WriteString(DSN,'SQL Server');
CloseKey;
if OpenKey('Software/ODBC/ODBC.INI/'+DSN,True) then
begin
if Database<>'' then
WriteString('Database',Database);
//默认数据库
WriteString('Server',Server);
WriteString('Driver',Driver);
WriteString('LastUser','sa');
if TrustConn then
WriteString('Trusted_Connection','Yes')
else
WriteString('Trusted_Connection','No');
end
else
begin
Result:=false;
Raise Exception.Create('建立ODBC数据源失败!');
end;
CloseKey;
end
else
begin
//创建键值失败
Result:=false;
Raise Exception.Create('建立ODBC数据源失败!');
end;
except
CloseKey;
FreeAndNil(RegTemp);
raise;
end;
RegTemp.CloseKey;
FreeAndNil(RegTemp);
end;
procedure ConnStr_BDE_FromADO_MSSQL(var BDEConnStr:WideString;
ADOConnStr:WideString;
ODBCDSN:string);
var Server,Database,Driver,UID,PWD:string;
TrustConn:boolean;
TempList:TStrings;
begin
//根据ADO连接参数生成BDE连接参数
TempList:=TStringList.Create;
try
TempList.Text:=BDEConnStr;
Server:=ConnStr_ADO_GetKeyVal(ADOConnStr,'Data Source');
Database:=ConnStr_ADO_GetKeyVal(ADOConnStr,'Initial Catalog');
UID:=ConnStr_ADO_GetKeyVal(ADOConnStr,'User ID');
PWD:=ConnStr_ADO_GetKeyVal(ADOConnStr,'Password');
TrustConn:=(Trim(ConnStr_ADO_GetKeyVal(ADOConnStr,'Integrated Security'))<>'');
if ODBCDSN='' then
ODBCDSN:=TempList.Values['ODBC DSN'];
if ODBCDSN='' then
ODBCDSN:=Server;
//获取默认连接参数
if Trim(TempList.Text)='' then
begin
ReturnDefaultConnStr('','SQL Server',TempList);
end;
//参数赋值
TempList.Values['SERVER NAME']:=Server;
TempList.Values['DATABASE NAME']:=Database;
TempList.Values['USER NAME']:=UID;
TempList.Values['PASSWORD']:=PWD;
TempList.Values['ODBC DSN']:=ODBCDSN;
Create_ODBC_DSN_MSSQL(ODBCDSN,Server,Database,TrustConn);
BDEConnStr:=TempList.Text;
finally
FreeAndNil(TempList);
end;
end;