odbc
const
ODBC_ADD_DSN = 1;
ODBC_CONFIG_DSN = 2;
ODBC_REMOVE_DSN = 3;
ODBC_ADD_SYS_DSN = 4;
ODBC_CONFIG_SYS_DSN = 5;
ODBC_REMOVE_SYS_DSN = 6;function SQLConfigDataSource(hwndParent: Integer;
fRequest: LongInt; lpszDriverString: string;
lpszAttributes: string): LongBool; stdcall; external 'ODBCCP32.DLL';procedure TForm1.Button1Click(Sender: TObject; var Key: Word;
Shift: TShiftState);
const
BoolStr: array[Boolean] of string = ('f', 't');
begin
Caption := BoolStr[
SQLConfigDataSource(0, ODBC_ADD_SYS_DSN,
// ~换成Handle就是手工配置
'SQL Server', //数据库类型
'DSN=MyDatabase'#0 + //数据源名称
'Server=(local)'#0 + //SQL Server服务器名
'Database=DatabaseName'#0 + //数据库名称
'Description=动态配置ODBC'#0 //描述
)];
end;
const
ODBC_ADD_DSN = 1;
ODBC_CONFIG_DSN = 2;
ODBC_REMOVE_DSN = 3;
ODBC_ADD_SYS_DSN = 4;
ODBC_CONFIG_SYS_DSN = 5;
ODBC_REMOVE_SYS_DSN = 6;function SQLConfigDataSource(hwndParent: Integer;
fRequest: LongInt; lpszDriverString: string;
lpszAttributes: string): LongBool; stdcall; external 'ODBCCP32.DLL';procedure TForm1.Button1Click(Sender: TObject; var Key: Word;
Shift: TShiftState);
const
BoolStr: array[Boolean] of string = ('f', 't');
begin
Caption := BoolStr[
SQLConfigDataSource(0, ODBC_ADD_SYS_DSN,
// ~换成Handle就是手工配置
'SQL Server', //数据库类型
'DSN=MyDatabase'#0 + //数据源名称
'Server=(local)'#0 + //SQL Server服务器名
'Database=DatabaseName'#0 + //数据库名称
'Description=动态配置ODBC'#0 //描述
)];
end;
var wReg:TRegistry;
tmppath:array[0..255] of char;
syspath:string;
tmpDBpath:string;
tpath:string;
const
ProMsg='测试ODBC数据源';
begin
GetSystemDirectory(tmppath,255);
syspath:=strpas(tmppath);
tpath:=ExtractFilePath(Application.ExeName);
tmpDBpath:=tpath+'work';
wReg:=TRegistry.Create;
with wReg do
begin
RootKey:=HKEY_LOCAL_MACHINE;
if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',true) then
writestring('RegTest','microsoft visual foxpro driver')
else
begin
application.MessageBox('ODBC初始化错误!',ProMsg,MB_OK) ;
result:=false;
EXIT;
end;
closekey;
if openkey('Software\ODBC\ODBC.INI\RegTest',true)then
begin
WriteString('backgroundFetch','yes');
WriteString('collate','STROKE');
WriteString('Deleted','yes');
WriteString('Description','vfp数据源');
WriteString('Driver',SysPath+'\vfpodbc.dll');
WriteString('exclusive','no');
WriteString('SetNoCountOn','no');
WriteString('SourceDB',tmpDBpath);
WriteString('sourceType','DBF');
end
else
begin
application.MessageBox('ODBC初始化错误!',ProMsg,mb_ok);
result:=false;
EXIT;
end;
closekey;
end;
result:=true;
end;
const
ODBC_ADD_SYS_DSN = 4;
function SQLConfigDataSource(hwndParent: Integer; fRequest: Integer;
lpszDriverString: String; lpszAttributes: String): Integer;
stdcall;external 'ODBCCP32.DLL'function CreateAccessODBC(DSN, DataBase, Description:string):Integer;
begin
Result:=SQLConfigDataSource(0, ODBC_ADD_SYS_DSN,
'Microsoft Access Driver (*.mdb)',
'DSN=' + DSN + chr(0) +
'DBQ=' + DataBase +chr(0) +
'Description=' + Description + chr(0)
);
end;
begin
RootKey:=HKEY_LOCAL_MACHINE;
if OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',True) then
begin //注册一个DSN名称
WriteString( 'partydb_C', 'Microsoft Access Driver (*.mdb)' );
end;
CloseKey;
if OpenKey('Software\ODBC\ODBC.INI\partydb_C',True) then
begin
WriteString( 'DBQ',filepath+'data\partydb' );//数据库目录
WriteString( 'Description','本地partydb数据源' );//数据源描述
WriteString( 'Driver', 'C:\WINDOWS\SYSTEM\ODBCJT32.DLL' );//驱动程序DLL文件
WriteInteger( 'DriverId', 25 );
//驱动程序标识
WriteString( 'FIL', 'Ms Access;' );
//Filter依据
WriteInteger( 'SafeTransactions', 0 );
//支持的事务操作数目
WriteString( 'UID', '' );//用户名称
end;
CloseKey; if OpenKey('Software\ODBC\ODBC.INI\partydb_C\Engines\Jet',True) then
begin
WriteString( 'ImplicitCommitSync', '' );
WriteInteger( 'MaxBufferSize', 2048 );//缓冲区大小
WriteInteger( 'PageTimeout', 5 );//页超时
WriteInteger( 'Threads', 3 );//支持的线程数目
WriteString( 'UserCommitSync', 'Yes' );
end;
CloseKey;
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;