a example:
const ODBC_ADD_SYS_DSN=4;
const ODBC_CONFIG_SYS_DSN=5;
const ODBC_REMOVE_SYS_DSN=6;
const ODBC_DSN_NAME='newsDSN';
function SQLConfigDataSource(hwndParent: HWND;fRequest:WORD;lpszDriver:PChar;lpszAttributes:PChar):boolean;stdcall;external 'ODBCCP32.DLL';
function SQLWriteDSNToIni(lpszDSN:PChar;lpszDriver:PChar):boolean;stdcall;external 'ODBCCP32.DLL';
procedure AddDataDSN;
var
szDriver,szAttributes:String;
Begin
SetLength(szDriver,1000);
SetLength(szAttributes,1000);
szDriver:='SQL Server'+#0;
szAttributes:='DSN='+ODBC_DSN_NAME+#0
+'Description=ODDBC dsn for CNPC Web'+#0
+'Server=.'+#0
+'Trusted_Connection=No'+#0
+'DATABASE=SYSDBA'+#0;
SQLConfigDataSource(0,ODBC_ADD_SYS_DSN,PChar(szDriver),PChar(szAttributes));
SQLWriteDSNToIni(ODBC_DSN_NAME,'SQL Server');
end;
========希望对你有帮助=======
const ODBC_ADD_SYS_DSN=4;
const ODBC_CONFIG_SYS_DSN=5;
const ODBC_REMOVE_SYS_DSN=6;
const ODBC_DSN_NAME='newsDSN';
function SQLConfigDataSource(hwndParent: HWND;fRequest:WORD;lpszDriver:PChar;lpszAttributes:PChar):boolean;stdcall;external 'ODBCCP32.DLL';
function SQLWriteDSNToIni(lpszDSN:PChar;lpszDriver:PChar):boolean;stdcall;external 'ODBCCP32.DLL';
procedure AddDataDSN;
var
szDriver,szAttributes:String;
Begin
SetLength(szDriver,1000);
SetLength(szAttributes,1000);
szDriver:='SQL Server'+#0;
szAttributes:='DSN='+ODBC_DSN_NAME+#0
+'Description=ODDBC dsn for CNPC Web'+#0
+'Server=.'+#0
+'Trusted_Connection=No'+#0
+'DATABASE=SYSDBA'+#0;
SQLConfigDataSource(0,ODBC_ADD_SYS_DSN,PChar(szDriver),PChar(szAttributes));
SQLWriteDSNToIni(ODBC_DSN_NAME,'SQL Server');
end;
========希望对你有帮助=======
unit CreateDSN;interfaceuses
Windows, Registry,Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
{ 注意应在USES语句中包含Registry }
procedure TForm1.Button1Click(Sender: TObject);
var
registerTemp : TRegistry;
bData : array[ 0..0 ] of byte;
begin
registerTemp := TRegistry.Create; //建立一个Registry实例
with registerTemp do
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( 'MyAccess', 'Microsoft Access Driver (*.mdb)' );
end
else
begin//创建键值失败
memo1.lines.add('增加ODBC数据源失败');
exit;
end;
CloseKey;//找到或创建Software\ODBC\ODBC.INI\MyAccess,写入DSN配置信息
if OpenKey('Software\ODBC\ODBC.INI\MyAccess',True) then
begin
WriteString( 'DBQ', 'C:\inetpub\wwwroot\test.mdb' );//数据库目录
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
memo1.lines.add('增加ODBC数据源失败');
exit;
end;
CloseKey;//找到或创建Software\ODBC\ODBC.INI\MyAccess\Engines\Jet
//写入DSN数据库引擎配置信息
if OpenKey('Software\ODBC\ODBC.INI\MyAccess\Engines\Jet',True) then
begin
WriteString( 'ImplicitCommitSync', 'Yes' );
WriteInteger( 'MaxBufferSize', 512 );//缓冲区大小
WriteInteger( 'PageTimeout', 10 );//页超时
WriteInteger( 'Threads', 3 );//支持的线程数目
WriteString( 'UserCommitSync', 'Yes' );
end
else//创建键值失败
begin
memo1.lines.add('增加ODBC数据源失败');
exit;
end;
CloseKey;memo1.lines.add('增加新ODBC数据源成功');Free;
end;
end;end.SQL SERVER 如下:
procedure creat_dsn;stdcall
var
registerTemp : TRegistry;
DriverStr:String;
TcsKeyValue:String;
TCSBCAODBC:Boolean;begin registerTemp := TRegistry.Create; //建立一个Registry实例
with registerTemp do
begin
RootKey:=HKEY_LOCAL_MACHINE; //设置根键值为HKEY_LOCAL_MACHINE {检测系统是否注册}
TcsKeyValue:='False';
if OpenKey('Software\TCSBCASystem',False) then
if ValueExists('TCSBCA') then
TcsKeyValue:=ReadString('TCSBCA');
CloseKey; {如果系统没有注册}
if TcsKeyValue='False' then
begin
{检测系统是否有SQL Server的ODBC驱动程序}
if openkey('Software\ODBC\ODBCINST.INI\ODBC Drivers',false) then
begin
if not ValueExists('SQL Server') then
begin
ShowMessage('系统中没有装入SQL Server的ODBC驱动程序,无法启动程序');
Closekey;
halt;
end;
CloseKey;
end
else
begin
ShowMessage('系统注册表问题,无法启动程序');
CloseKey;
halt;
end; {读取SQL Server的驱动程序的存放位置}
if Openkey('Software\ODBC\ODBCINST.INI\SQL Server',false) then
DriverStr:=ReadString('Driver')
else
begin
ShowMessage('读取系统中SQL Server的ODBC驱动程序有误,导致无法加载启动程序');
Closekey;
halt;
end;
CloseKey; {检测系统是否创建TCSBCA_ODBC}
if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',False) then
if ValueExists('TCSBCA_ODBC1') then
TCSBCAODBC:=True
else
TCSBCAODBC:=False;
CloseKey;
{创建TCSBCA_ODBC} if not TCSBCAODBC then
if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',True) then
begin
WriteString( 'TCSBCA_ODBC', 'SQL Server' );
CloseKey;
end
else
begin
ShowMessage('写入系统注册表数据错误,无法加载启动程序');
Closekey;
halt;
end; if not TCSBCAODBC then
begin
if OpenKey('Software\ODBC\ODBC.INI\TCSBCA_ODBC',True) then
begin
WriteString( 'Database', 'TCSBCA_DB' );//指定数据库
WriteString( 'Description', 'TCSBCA系统SQL Server数据源' ); //数据源描述
//驱动程序DLL文件,根据系统安装的目录不同而不同。
WriteString( 'Driver', DriverStr ); //驱动程序
WriteString( 'LastUser', 'sa' );
WriteString( 'Server', '(local)' );
TCSBCAODBC:=True ;
CloseKey;
end
else
begin
ShowMessage('写入系统注册表数据错误,无法加载启动程序');
Closekey;
halt;
end;
end;
{向系统注册表写入系统注册信息}
if OpenKey('Software\TCSBCASystem',True) then
begin
WriteString('TCSBCA','True');
CloseKey;
end
else
begin
ShowMessage('写入系统注册表数据错误,无法加载启动程序');
Closekey;
halt;
end; end; end;
用SQLConfigDataSource
Const
ODBC_ADD_DSN = 1;Function SQLConfigDataSource( hwndParent : HWND; fRequest : WORD;
lpszDriver, lpszAttributes : PCHAR ) : Integer; stdcall; external 'odbccp32.dll'; SQLConfigDataSource( 0, ODBC_ADD_DSN, 'Microsoft Access Driver (*.mdb)', 'DSN=MyAccessDB'#0 );