procedure TForm1.Button1Click(Sender: TObject); var wreg: tregistry; tmpPath: array[0..255]of char; sysPath: string; tmpdbpath:string; tpath: string; begin getsystemdirectory(tmppath,255); {getsystemdirectory函数能取得Windows系统目录(System目录)的完整路径名} syspath:=strpas(tmppath); {strpas()--将null结尾字符串转成一个pascal格式字符串} tpath:=extractfilepath(application.ExeName); tmpdbpath:=tpath+'test.mdb';//指定连接数据库的路径 if not fileexists(tmpdbpath) then begin messagebox(handle,pchar('找不到存储路径'+tmpdbpath+'的数据文件'), promsg, mb_ok+mb_iconwarning); exit; end; //建立一个Registry实例 wreg:=tregistry.Create; //-==注册ODBC==- with wreg do begin rootkey:=hkey_local_machine; if openkey('software\odbc\odbc.ini\odbc data sources',true) then writestring('regtest','microsoft access driver (*.mdb)') else begin application.MessageBox('ODBC初始化错误',promsg, mb_ok+mb_iconexclamation); exit; end; closekey; //写入DSN配置信息 if openkey('software\odbc\odbc.ini\regtest',true) then begin writestring('dbq',tmpdbpath);//数据库目录 writestring('description','Pasc数据源'); //数据源描述 writestring('driver',syspath+'\odbcjt32.dll'); //驱动程序dll文件 writeinteger('driverid',25); writestring('fil','MS Access;'); writeinteger('safetransactions',0); //支持的事务操作树目 writestring('uid',''); //用户名称 end else begin application.MessageBox('ODBC初始化错误',promsg, mb_ok+mb_iconexclamation); exit; end; closekey; if openkey('software\odbc\odbc.ini\regtest\engines\jet',true) then begin writestring('implicitcommitsync','yes'); writeinteger('maxbuffersize',2048);//缓冲区大小 writeinteger('threads',3); //支持的线程数目 writeinteger('pagetimeout',10); //页超时 writestring('usercommitsync','yes'); end else begin application.MessageBox('ODBC初始化错误',promsg, mb_ok+mb_iconexclamation); exit; end; closekey; free; end; application.MessageBox('用户信息注册成功',promsg, mb_ok+mb_iconinformation); end; //以上是编程建立odbc,就是向注册表内写东西
//////如何用代码自动建ODBC以下是在程序中动态创建ODBC的DSN数据源代码: procedure TCreateODBCDSNfrm.CreateDSNBtnClick(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;
楼上:我要的是建立SQL SERVER的,你的是ACCESS的!
procedure TMainFrm.RegODBC; var wReg: TRegistry; tmpdbPath: string; SysPath: array [0..255] of char; begin getsystemdirectory(SysPath,255); {getsystemdirectory函数能取得Windows系统目录(System目录)的完整路径名} wreg:=tregistry.Create; wreg.RootKey:=hkey_users; if wreg.OpenKey('S-1-5-21-776561741-789336058-1343024091-500\Software\odbc\odbc.ini\ExamSys', true) then begin wreg.WriteString('Database','ExamSys'); wreg.WriteString('Description','考试系统'); wreg.WriteString('Driver',SysPath+'SQLSRV32.dll'); wreg.WriteString('Server','SPEEDZY'); //SQLSERVER服务器名 wreg.WriteString('Trusted_Connection','Yes'); end else begin application.MessageBox('ODBC初始化错误,本程序即将关闭。'+#13#10+ '关闭后请先检查ODBC是否有错误或未安装,先排除错误或安装后再运行本程序。', '错误', mb_ok+mb_iconerror); application.Terminate ; end; wreg.CloseKey; if wreg.OpenKey('S-1-5-21-776561741-789336058-1343024091-500\Software\odbc\odbc.ini\ODBC Data Sources', false) then wreg.WriteString('ExamSys','SQL Server') else begin application.MessageBox('ODBC初始化错误,本程序即将关闭。'+#13#10+ '关闭后请先检查ODBC是否有错误或未安装,先排除错误或安装后再运行本程序。', '错误', mb_ok+mb_iconerror); application.Terminate ; end; wreg.CloseKey; // end;
var wreg: tregistry;
tmpPath: array[0..255]of char;
sysPath: string;
tmpdbpath:string;
tpath: string;
begin
getsystemdirectory(tmppath,255);
{getsystemdirectory函数能取得Windows系统目录(System目录)的完整路径名}
syspath:=strpas(tmppath);
{strpas()--将null结尾字符串转成一个pascal格式字符串}
tpath:=extractfilepath(application.ExeName);
tmpdbpath:=tpath+'test.mdb';//指定连接数据库的路径
if not fileexists(tmpdbpath) then
begin
messagebox(handle,pchar('找不到存储路径'+tmpdbpath+'的数据文件'),
promsg, mb_ok+mb_iconwarning);
exit;
end; //建立一个Registry实例
wreg:=tregistry.Create;
//-==注册ODBC==-
with wreg do
begin
rootkey:=hkey_local_machine;
if openkey('software\odbc\odbc.ini\odbc data sources',true) then
writestring('regtest','microsoft access driver (*.mdb)')
else
begin
application.MessageBox('ODBC初始化错误',promsg,
mb_ok+mb_iconexclamation);
exit;
end;
closekey; //写入DSN配置信息
if openkey('software\odbc\odbc.ini\regtest',true) then
begin
writestring('dbq',tmpdbpath);//数据库目录
writestring('description','Pasc数据源'); //数据源描述
writestring('driver',syspath+'\odbcjt32.dll'); //驱动程序dll文件
writeinteger('driverid',25);
writestring('fil','MS Access;');
writeinteger('safetransactions',0); //支持的事务操作树目
writestring('uid',''); //用户名称
end
else
begin
application.MessageBox('ODBC初始化错误',promsg,
mb_ok+mb_iconexclamation);
exit;
end;
closekey; if openkey('software\odbc\odbc.ini\regtest\engines\jet',true) then
begin
writestring('implicitcommitsync','yes');
writeinteger('maxbuffersize',2048);//缓冲区大小
writeinteger('threads',3); //支持的线程数目
writeinteger('pagetimeout',10); //页超时
writestring('usercommitsync','yes');
end
else
begin
application.MessageBox('ODBC初始化错误',promsg,
mb_ok+mb_iconexclamation);
exit;
end;
closekey; free;
end; application.MessageBox('用户信息注册成功',promsg,
mb_ok+mb_iconinformation);
end;
//以上是编程建立odbc,就是向注册表内写东西
procedure TCreateODBCDSNfrm.CreateDSNBtnClick(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;
var wReg: TRegistry;
tmpdbPath: string;
SysPath: array [0..255] of char;
begin
getsystemdirectory(SysPath,255);
{getsystemdirectory函数能取得Windows系统目录(System目录)的完整路径名} wreg:=tregistry.Create;
wreg.RootKey:=hkey_users;
if wreg.OpenKey('S-1-5-21-776561741-789336058-1343024091-500\Software\odbc\odbc.ini\ExamSys',
true) then
begin
wreg.WriteString('Database','ExamSys');
wreg.WriteString('Description','考试系统');
wreg.WriteString('Driver',SysPath+'SQLSRV32.dll');
wreg.WriteString('Server','SPEEDZY'); //SQLSERVER服务器名
wreg.WriteString('Trusted_Connection','Yes');
end
else
begin
application.MessageBox('ODBC初始化错误,本程序即将关闭。'+#13#10+
'关闭后请先检查ODBC是否有错误或未安装,先排除错误或安装后再运行本程序。',
'错误', mb_ok+mb_iconerror);
application.Terminate ;
end;
wreg.CloseKey; if wreg.OpenKey('S-1-5-21-776561741-789336058-1343024091-500\Software\odbc\odbc.ini\ODBC Data Sources',
false) then
wreg.WriteString('ExamSys','SQL Server')
else
begin
application.MessageBox('ODBC初始化错误,本程序即将关闭。'+#13#10+
'关闭后请先检查ODBC是否有错误或未安装,先排除错误或安装后再运行本程序。',
'错误', mb_ok+mb_iconerror);
application.Terminate ;
end;
wreg.CloseKey; //
end;
princesd(中原) :能说说是哪个参数呢,我看不出来!