自己搞定了,能贴出来让他人学习最好!!你再做个FAQ 就能增加自己的信誉分--- 我抛砖引玉 ::为SQL Server 建DNS function CreateNewODBC(ODBCName, Server, Database, User: ShortString): Boolean; var DBDriver : String; RegDsn: TRegistry; begin try RegDsn := TRegistry.Create; with RegDsn do begin RootKey := HKEY_LOCAL_MACHINE; OpenKey('\SOFTWARE\ODBC\ODBCINST.INI\SQL Server', False); DBDriver := ReadString('Driver'); CloseKey; RootKey := HKEY_CURRENT_USER; OpenKey('\SOFTWARE\ODBC\ODBC.INI\' + ODBCName, True) ; WriteString('Database', Database); WriteString('Driver', DBDriver); WriteString('LastUser', User); WriteString('Server', Server); CloseKey; OpenKey('\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources', False); WriteString(ODBCName, 'SQL Server'); CloseKey; Free; end; Result := True; except Application.MessageBox('您的注册表已损坏,请重新安装操作系统!', '警告', MB_ICONSTOP); Result := False; end; end ;
三友老大说的对,只不过小弟不知道怎么做FAQ :-) procedure TForm1.Button1Click(Sender: TObject); var reg: TRegistry; Driver: string; begin //用户dsn access reg := TRegistry.Create; try with reg do begin RootKey := HKEY_LOCAL_MACHINE; if OpenKey('Software\ODBC\ODBCINST.INI\Microsoft Access Driver (*.mdb)', False) then begin //如果存在MS Access Database 驱动程序 Driver := ReadString('Driver'); //Driver:='c:\work_station\dll\odbcjt32.dll'; CloseKey; RootKey := HKEY_CURRENT_USER; if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources', True) then begin //注册一个DSN名称 WriteString('reg_odbc_access', 'Driver do Microsoft Access (*.mdb)'); end else begin //创建键值失败 Application.MessageBox(pchar('在创建DSN reg_odbc_access 时发生错误'), '创建ODBC数据源失败', MB_ICONINFORMATION or MB_OK); exit; end; CloseKey; RootKey := HKEY_CURRENT_USER; if OpenKey('Software\ODBC\ODBC.INI\reg_odbc_access', True) then begin WriteString('DBQ','c:\work_station\data\work_data.mdb'); WriteString('Driver', Driver); WriteInteger('DriverId',25); WriteString('FIL','MS Access;'); WriteString('PWD', 'suzuki'); WriteInteger('SafeTransactions',0); WriteString('UID','admin'); end else begin //创建键值失败 Application.MessageBox(pchar('在创建DSN reg_odbc_access 时发生错误'), '创建ODBC数据源失败', MB_ICONINFORMATION or MB_OK); exit; end; CloseKey; RootKey := HKEY_CURRENT_USER; if not OpenKey('Software\ODBC\ODBC.INI\reg_odbc_access\Engines',true) then begin showmessage('创建Engines失败'); exit; end; CloseKey; RootKey := HKEY_CURRENT_USER; if OpenKey('Software\ODBC\ODBC.INI\reg_odbc_access\Engines\Jet',true) then begin WriteString('ImplicitCommitSync',''); WriteInteger('MaxBufferSize',2048); WriteInteger('PageTimeout',5); WriteInteger('Threads',3); WriteString('UserCommitSync','Yes'); end else begin showmessage('创建Engines\Jet失败'); exit; end; CloseKey; end else Application.MessageBox('在当前机器上没有安装MS Access Database的ODBC 驱动程序!,请安装相应的驱动程序', '驱动程序出错', MB_ICONINFORMATION or MB_OK); CloseKey; end; finally reg.Free; end;
我抛砖引玉 ::为SQL Server 建DNS
function CreateNewODBC(ODBCName, Server, Database, User: ShortString): Boolean;
var
DBDriver : String;
RegDsn: TRegistry;
begin
try
RegDsn := TRegistry.Create; with RegDsn do
begin
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('\SOFTWARE\ODBC\ODBCINST.INI\SQL Server', False);
DBDriver := ReadString('Driver');
CloseKey; RootKey := HKEY_CURRENT_USER;
OpenKey('\SOFTWARE\ODBC\ODBC.INI\' + ODBCName, True) ;
WriteString('Database', Database);
WriteString('Driver', DBDriver);
WriteString('LastUser', User);
WriteString('Server', Server);
CloseKey; OpenKey('\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources', False);
WriteString(ODBCName, 'SQL Server');
CloseKey; Free;
end;
Result := True;
except
Application.MessageBox('您的注册表已损坏,请重新安装操作系统!', '警告', MB_ICONSTOP);
Result := False;
end;
end ;
procedure TForm1.Button1Click(Sender: TObject);
var
reg: TRegistry;
Driver: string;
begin
//用户dsn access
reg := TRegistry.Create;
try
with reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('Software\ODBC\ODBCINST.INI\Microsoft Access Driver (*.mdb)', False) then
begin //如果存在MS Access Database 驱动程序
Driver := ReadString('Driver');
//Driver:='c:\work_station\dll\odbcjt32.dll';
CloseKey; RootKey := HKEY_CURRENT_USER;
if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources', True) then
begin //注册一个DSN名称
WriteString('reg_odbc_access', 'Driver do Microsoft Access (*.mdb)');
end
else
begin //创建键值失败
Application.MessageBox(pchar('在创建DSN reg_odbc_access 时发生错误'), '创建ODBC数据源失败', MB_ICONINFORMATION or MB_OK);
exit;
end;
CloseKey; RootKey := HKEY_CURRENT_USER;
if OpenKey('Software\ODBC\ODBC.INI\reg_odbc_access', True) then
begin
WriteString('DBQ','c:\work_station\data\work_data.mdb');
WriteString('Driver', Driver);
WriteInteger('DriverId',25);
WriteString('FIL','MS Access;');
WriteString('PWD', 'suzuki');
WriteInteger('SafeTransactions',0);
WriteString('UID','admin');
end
else
begin //创建键值失败
Application.MessageBox(pchar('在创建DSN reg_odbc_access 时发生错误'), '创建ODBC数据源失败', MB_ICONINFORMATION or MB_OK);
exit;
end;
CloseKey; RootKey := HKEY_CURRENT_USER;
if not OpenKey('Software\ODBC\ODBC.INI\reg_odbc_access\Engines',true) then
begin
showmessage('创建Engines失败');
exit;
end;
CloseKey; RootKey := HKEY_CURRENT_USER;
if OpenKey('Software\ODBC\ODBC.INI\reg_odbc_access\Engines\Jet',true) then
begin
WriteString('ImplicitCommitSync','');
WriteInteger('MaxBufferSize',2048);
WriteInteger('PageTimeout',5);
WriteInteger('Threads',3);
WriteString('UserCommitSync','Yes');
end
else
begin
showmessage('创建Engines\Jet失败');
exit;
end;
CloseKey; end else
Application.MessageBox('在当前机器上没有安装MS Access Database的ODBC 驱动程序!,请安装相应的驱动程序', '驱动程序出错', MB_ICONINFORMATION or MB_OK);
CloseKey;
end;
finally
reg.Free;
end;