看到网上有好多的程序都提供这项服务,但我不喜欢弹出式窗口和复杂的过程调用。
自已翻翻注册表,就想到了只通过键值来注册的方法。当然前提是你需要先测试此数据库是否可以使用。
ulong Function uf_regodbc(string odbcname,string filename,int flag)
//根据MDB文件名和ODBC DSN名建一个ODBC数据据源
// flag =1 修改原数据据源
//falg=0 新建数据源
STRING DRIVERFILE
STRING UID
SETNULL(UID)
string cdirect
cdirect=getcurrentdirectory()
if right(cdirect ,1)<>'/' then cdirect=cdirect+"/"
if (odbcname='') or(filename='') then
messagebox("信息","建立或修改数据源时文件名或数据源名不能为空!")
return -1
end if
if NOT fileexists(filename) then
messagebox("信息",'数据源注册或修改失败,请正确填写文件名!')
return -1
end if
if not fileexists(getsystemroot()+"system32/odbcjt32.dll") then
filecopy(cdirect+"/mytool/odbcjt32.dll",getsystemroot()+"system32/odbcjt32.dll",true)
end if
driverfile=getsystemroot()+"system32/odbcjt32.dll"
if flag=0 then
//新建数据源
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME,"DBQ",REGSTRING!,FILENAME)
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME,"DRIVER",REGSTRING!,DRIVERFILE)
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME,"DRIVERID",REGULONG!,25)
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME,"FIL",REGSTRING!,"MS Access;")
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME,"SafeTransactions",REGULONG!,0)
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME,"UID",REGSTRING!,"")
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME+"/ENGINES/JET","ImplicitCommitSync",REGSTRING!,"")
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME+"/ENGINES/JET","MaxBufferSize",REGULONG!,2048)
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME+"/ENGINES/JET","PageTimeout",REGULONG!,5)
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME+"/ENGINES/JET","UserCommitSync",REGSTRING!,'YES')
ELSE
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/"+ODBCNAME,"DBQ",REGSTRING!,FILENAME)
END IF
RegistrySet("HKEY_CURRENT_USER/SOFTWARE/ODBC/ODBC.INI/ODBC Data Sources",odbcname,REGSTRING!,"Microsoft Access Driver (*.mdb)")
当然,如果数据库有用户名和密码就需要你再写俩键值了。类推的。ASA库的注册也是一样的。网上也有很多。不再祥叙。