下面是增加access和SQL server的源码, 请参考:
unit CreateODBC;
interface
uses
Windows, SysUtils, Registry;
Function CreateSQLServerDsn(const FDSN,FServer,FUser:String):Integer;
Function CreateAccessDsn(const myDSN, strFileName:String):string;
Function CreateAccessUserDsn(const myDSN, strFileName:String):string;
implementation
Function CreateSQLServerDsn(const FDSN,FServer,FUser:String):Integer;
var
RegisterTemp:TRegistry;
s : string;
begin
RegisterTemp:=TRegistry.Create;
with RegisterTempdo
begin
RootKey:=HKEY_LOCAL_MACHINE;
//首先判断此DSN是否已经存在,如在,则不用继续下去
if OpenKey('Software/ODBC/ODBC.INI/'+FDSN,False) then
begin
CloseKey;
Result:=0;
Free;
exit;
end;
CloseKey;
//判断SQL Server的驱动程序是否已经安装
If OpenKey('Software/ODBC/ODBCINST.INI/ODBC Drivers',False) then
begin
s:=ReadString('SQL Server');
if UpperCase(S)<>'INSTALLED' then
begin
CloseKey;
Result:=2;
Free;
exit;
end;
end
else
begin
CloseKey;
Result:=3;
Free;
exit;
end;
CloseKey;
//寻找SQL Server的驱动程序路径
If OpenKey('Software/ODBC/ODBCINST.INI/SQL Server',False) then
begin
s:=ReadString('Driver');
if S='' then
begin
CloseKey;
Result:=2;
Free;
exit;
end;
end
else
begin
CloseKey;
Result:=2;
Free;
exit;
end;
CloseKey;
//注册一个DSN名称
If OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources',True) then
WriteString(FDSN,'SQL Server')
else
begin
CloseKey;
Result:=1;
Free;
exit;
end;
CloseKey;
if OpenKey('Software/ODBC/ODBC.INI/'+FDSN,True) then
begin
WriteString('Driver',S);
WriteString('LastUser',FUser);
WriteString('Server',FServer);
Result:=5;
end
else
begin
CloseKey;
Result:=1;
Free;
exit;
end;
CloseKey;
Free;
end;
end;
// Result:=0;
// Result:='此ODBC 的DSN已经存在,创建失败';
// Result:=3;
// Result:='操作系统中ODBC驱动程序没有安装,请检查控制面板的ODBC设置是否存在?';
// Result:=2;
// Result:='Access的ODBC驱动程序没有安装';
// Result:=1;
// Result:='ODBC驱动程序没有安装';
// result :=5;
// result :='成功' ;
Function CreateAccessDsn(const myDSN, strFileName:String):string;
var
registerTemp : TRegistry;
bData : array[ 0..0 ] of byte;
begin
registerTemp := TRegistry.Create;
//建立一个Registry实例
with registerTempdo
begin
RootKey:=HKEY_LOCAL_MACHINE;//设置根键值为HKEY_LOCAL_MACHINE
//找到Software/ODBC/ODBC.INI/ODBC Data Sources
if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources',True) then
begin
//注册一个DSN名称
WriteString( myDSN, 'Microsoft Access Driver (*.mdb)' );
end
else
begin
//创建键值失败
Result :='增加ODBC数据源失败';
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/MyAccess,写入DSN配置信息
if OpenKey('Software/ODBC/ODBC.INI/'+MyDSN,True) then
begin
WriteString( 'DBQ', strFileName);//数据库目录
WriteString( 'Description', '我的新数据源' );//数据源描述
WriteString( 'Driver', 'C:/PWIN98/SYSTEM/odbcjt32.dll' );//驱动程序DLL文件
WriteInteger( 'DriverId', 25 );//驱动程序标识
WriteString( 'FIL', 'Ms Access;' );//Filter依据
WriteInteger( 'SafeTransaction', 0 );//支持的事务操作数目
WriteString( 'UID', '' );//用户名称
bData[0] := 0;
WriteBinaryData( 'Exclusive', bData, 1 );//非独占方式
WriteBinaryData( 'ReadOnly', bData, 1 );//非只读方式
end
else
//创建键值失败
begin
Result :='增加ODBC数据源失败';
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/MyAccess/Engines/Jet
//写入DSN数据库引擎配置信息
if OpenKey('Software/ODBC/ODBC.INI/'+MyDSN+'/Engines/Jet',True) then
begin
WriteString( 'ImplicitCommitSync', 'Yes' );
WriteInteger( 'MaxBufferSize', 512 );//缓冲区大小
WriteInteger( 'PageTimeout', 10 );//页超时
WriteInteger( 'Threads', 3 );//支持的线程数目
WriteString( 'UserCommitSync', 'Yes' );
end
else
//创建键值失败
begin
Result :='增加ODBC数据源失败';
exit;
end;
CloseKey;
Result :='增加新ODBC数据源成功';
Free;
end;
end;
Function CreateAccessUserDsn(const myDSN, strFileName:String):string;
var
registerTemp : TRegistry;
bData : array[ 0..0 ] of byte;
begin
registerTemp := TRegistry.Create;
//建立一个Registry实例
with registerTempdo
begin
// RootKey:=HKEY_LOCAL_MACHINE;//设置根键值为HKEY_LOCAL_MACHINE
RootKey:=HKEY_Current_USER;//设置根键值为HKEY_LOCAL_MACHINE
//找到Software/ODBC/ODBC.INI/ODBC Data Sources
if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources',True) then
begin
//注册一个DSN名称
WriteString( myDSN, 'Microsoft Access Driver (*.mdb)' );
end
else
begin
//创建键值失败
Result :='增加ODBC数据源失败';
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/MyAccess,写入DSN配置信息
if OpenKey('Software/ODBC/ODBC.INI/'+MyDSN,True) then
begin
WriteString( 'DBQ', strFileName);//数据库目录
WriteString( 'Description', '我的新数据源' );//数据源描述
WriteString( 'Driver', 'C:/PWIN98/SYSTEM/odbcjt32.dll' );//驱动程序DLL文件
WriteInteger( 'DriverId', 25 );//驱动程序标识
WriteString( 'FIL', 'Ms Access;' );//Filter依据
WriteInteger( 'SafeTransaction', 0 );//支持的事务操作数目
WriteString( 'UID', '' );//用户名称
bData[0] := 0;
WriteBinaryData( 'Exclusive', bData, 1 );//非独占方式
WriteBinaryData( 'ReadOnly', bData, 1 );//非只读方式
end
else
//创建键值失败
begin
Result :='增加ODBC数据源失败';
exit;
end;
CloseKey;
//找到或创建Software/ODBC/ODBC.INI/MyAccess/Engines/Jet
//写入DSN数据库引擎配置信息
if OpenKey('Software/ODBC/ODBC.INI/'+MyDSN+'/Engines/Jet',True) then
begin
WriteString( 'ImplicitCommitSync', 'Yes' );
WriteInteger( 'MaxBufferSize', 512 );//缓冲区大小
WriteInteger( 'PageTimeout', 10 );//页超时
WriteInteger( 'Threads', 3 );//支持的线程数目
WriteString( 'UserCommitSync', 'Yes' );
end
else
//创建键值失败
begin
Result :='增加ODBC数据源失败';
exit;
end;
CloseKey;
Result :='增加新ODBC数据源成功';
Free;
end;
end;
end.
调用方法如下:
procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
begin
i :=CreateSQLServerDsn(trim(Edit1.Text), trim(Edit3.Text),trim(Edit4.Text));
if i= 5 then
ShowMessage('成功');
if i= 0 then
ShowMessage('失败:'+IntToStr(i));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
strTemp : string;
begin
strTemp:=CreateAccessUserDsn(trim(Edit1.Text), trim(FileNameEdit2.Text));
ShowMessage(strTemp);
end;
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
FileNameEdit2.Visible :=false;
Edit3.Visible :=true;
Edit4.Visible :=true;
Label2.Visible :=false;
Label3.Visible :=true;
Label4.Visible :=true;
end;
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
FileNameEdit2.Visible :=true;
Edit3.Visible :=false;
Edit4.Visible :=false;
Label2.Visible :=true;
Label3.Visible :=false;
Label4.Visible :=false;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
strTemp : string;
begin
strTemp:=CreateAccessDsn(trim(Edit1.Text), trim(FileNameEdit2.Text));
ShowMessage(strTemp);
end;