对于网上有关内容进行整理,动态的本质在于手动控制ODBC数据源在注册表里的数值!可直接操作注册表也可通过DLL文件操作。
ODBC在注册表中的存储位置:
可用的驱动程序在 HKEY_LOCAL_MACHINE/SOFTWARE/ODBC/ODBCINST.INI 下,
System DSN 在 HKEY_LOCAL_MACHINE/SOFTWARE/ODBC/ODBC.INI 下,
User DSN 在 HKEY_CURRENT_USER/Software/ODBC/ODBC.INI 下.
注册表:
在DELPHI程序中动态设置ODBC数据源(创建示例)
http://www.builder.com.cn/2007/1102/600694.shtml
Delphi 5 数据库应用中ODBC数据源的自动管理(修改示例)
http://www.lunwentianxia.com/product.free.5031040.1/
上面各网贴的简化代码例子:
example(http://topic.csdn.net/t/20020705/14/852704.html)
var Reg:TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('/Software/ODBC/ODBC.INI', True) then
begin
if not reg.KeyExists('/Software/ODBC/ODBC.INI/pcPPOLoad') then
begin
Reg.CreateKey('pcPPOLoad');
Reg.OpenKey('/Software/ODBC/ODBC.INI/pcPPOLoad', True);
// Reg.WriteString('','');
Reg.WriteString('Database','PlanningDB');
Reg.WriteString('Driver','C:/WINNT/System32/SQLSRV32.dll');
Reg.WriteString('LastUser','ODMWRITE');
Reg.WriteString('Server','GET-ITTEST02');
Reg.CloseKey;
end;
reg.OpenKey('/Software/ODBC/ODBC.INI/ODBC Data Sources',true);
reg.WriteString('pcPPOLoad','SQL Server');
reg.CloseKey;
end;
finally
Reg.Free;
inherited;
end;
DLL文件:
example A(http://topic.csdn.net/t/20020505/10/699027.html):
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;
example B(http://topic.csdn.net/t/20020705/14/852704.html):
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, //0,不会弹出配置窗口,换成Handle就是手工配置,会弹出标准的ODBC配置窗口。
ODBC_ADD_SYS_DSN,
'SQL Server', //数据库类型
'DSN=MyDatabase'#0 + //数据源名称
'Server=(local)'#0 + //SQL Server服务器名
'Database=DatabaseName'#0 + //数据库名称
'Description=动态配置ODBC'#0 //描述
)];
end;
注:A与B相似,可做互补!
通常我会在做INSTALLSHIELD的时候,
附:VC++等相关问题解答
1,如何在VC中动态设置ODBC数据源?
http://topic.csdn.net/t/20000821/11/26093.html
2,odbc中动态绑定域的方法
用ODBC API函数SQLBindCol可,申明如下:
SQLRETURN SQLBindCol(
SQLHSTMTStatementHandle,
SQLUSMALLINTColumnNumber,
SQLSMALLINTTargetType,
SQLPOINTERTargetValuePtr,
SQLINTEGERBufferLength,
SQLINTEGER *StrLen_or_IndPtr);
祥见MSDN
3,如何动态设置ODBC数据源
网址:http://topic.csdn.net/t/20020721/01/889991.html
摘要:
需要文件 odbcinst.h(bcb或vc的include目录) odbccp32.dll(winnt/system32/目录) implib.exe(通过dll生成lib库) coff2omf.exe (将coff库转化为omf库)
首先将上面文件拷贝到工程目录,
执行下面命令:
implib.exe odbccp32.lib odbccp32.dll
coff2omf Odbccp32.lib
下面程序的功能为当应用程序启动时,动态添加/修改一个指向当前目录的,名为test的系 统数据源。
新建一个工程,在工程主文件(含有winmain函数)中添加
#include
USELIB("odbccp32.lib");//lib在工程当前目录
修改后的代码如下;
//---------------------------------------------------------------------------
#include
#include
#pragma hdrstop
USEFORM("main.cpp", Form1);
//工程的其他单元申明.....
USELIB("odbccp32.lib");
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
AnsiString Data,DataPath,dbpath;
unsigned char temp[255];//接收转化后的连接字符串
LPSTR ch;//字符串指针相当于char *
DataPath=GetCurrentDir();//获取当前目录
Data=DataPath+"//test.mdb/b";//假定当前数据库为test.mdb
dbpath="DSN=test/bDBQ="+Data+"DEFAULTDIR="+DataPath+"/b/b";
//构造odbc连接字符串,test为数据源名,非法字符先用/b代替
ch=dbpath.c_str();//将AnsiString转化为字符串
int i=0;
while(*ch!='/0')//查找并替换非法字符,并装入字符数组
{
if(*ch=='/b')
*ch='/0';
temp[i]=*ch;
ch++;
i++;
}
if(::SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,(LPSTR)"Microsoft Access Driver (*.mdb)/0",(LPSTR)temp))//修改或添加数据源,如果成功,程序正常启动
{
Application->Title = "动态设定odbc数据源";
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
else
{
ShowMessage("数据库设定失败!");
Application->Terminate();
}
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
//---------------------------------------------------------------------------