关于动态创建ODBC数据源:
首先声明一个API函数:
Function SQLConfigDataSource(hwndParent,fRequest:Longint;lpszDriver:string;lpszAttributes:string):Longint; stdcall;external 'ODBCCP32.DLL';
//添加一个Access数据源
function AddAccessODBC(SourceName,DatabaseName,Desc,password:string):boolean;
var intRet:longint;
strDriver:string;
strAttributes: string;
begin
strDriver:= 'Microsoft Access Driver (*.mdb)';// 'Set the attributes delimited by null.
strAttributes:= ('UID=admin'#0'DESCRIPTION='+Desc+#0'UserCommitSync=Yes'#0'Threads=3'#0'SafeTransactions=0'#0'PageTimeout=5'#0'MaxScanRows=8'#0'DSN='+SourceName+#0'MaxBufferSize=512'#0'ImplicitCommitSync=Yes'#0'FIL=MS Access'#0'DriverId=25'#0'DBQ='+DatabaseName+#0'PWD='+password+#0);
//上面的默认参数你可以自己修改
intRet:= SQLConfigDataSource(0, ODBC_ADD_DSN, strDriver, strAttributes);
result:=(intRet<>0);
end;
//删除一个数据源
function DelAccessODBC(SourceName,DatabaseName,Desc,password:string):boolean;
var intRet:longint;
strDriver:string;
strAttributes: string;
begin
strDriver:= 'Microsoft Access Driver (*.mdb)';// 'Set the attributes delimited by null.
strAttributes:= ('UID=admin'#0'DESCRIPTION='+Desc+#0'UserCommitSync=Yes'#0'Threads=3'#0'SafeTransactions=0'#0'PageTimeout=5'#0'MaxScanRows=8'#0'DSN='+SourceName+#0'MaxBufferSize=512'#0'ImplicitCommitSync=Yes'#0'FIL=MS Access'#0'DriverId=25'#0'DBQ='+DatabaseName+#0'PWD='+password+#0);
intRet:= SQLConfigDataSource(0, ODBC_REMOVE_DSN, strDriver, strAttributes);
result:=(intRet<>0);
end;