想用BDE导Access数据库,要创建ODBC 数据源。找了半天也没见着BCB写的代码,所以我改写了一个。如有不足处,请指正。 [email protected]
/* 函数功能:创建Access ODBC数据源 输入参数:DSN--------所要创建的 Access ODBC数据源的名称 strMDBFile-----------Access 库文件的完整路径 strDesc---------这个数据源的描述 strLoginUser---------登录strMDBFile所指的文件的用户名 strPassword----------登录strMDBFile所指的文件的密码 返回值 :成功返回true, 否则会抛出Exception (这样写会不会有问题呀?) 用 法: (1)CreateAccessDSN("jll_access", "c:\\windows\\desktop\\starso.mdb"); (2)CreateAccessDSN("jll_access2", "c:\\windows\\desktop\\starso.mdb", " ", "Admin", "ok_pwd"); 注 意: #include <Registry.hpp> #include <memory>
using namespace std; */ bool __fastcall CreateAccessDSN(const AnsiString& DSN, const AnsiString& strMDBFile, const AnsiString& strDesc = "no descript", const AnsiString& strLoginUser = "", const AnsiString& strPassword = "") { auto_ptr<TRegistry> spReg(new TRegistry()); spReg->RootKey = HKEY_LOCAL_MACHINE; //设置根键值为HKEY_LOCAL_MACHINE //找到Software\ODBC\ODBC.INI\ODBC Data Sources if (spReg->OpenKey("Software\\ODBC\\ODBC.INI\\ODBC Data Sources", true)) {//注册一个DSN名称 spReg->WriteString(DSN, "Microsoft Access Driver (*.mdb)" ); spReg->CloseKey(); }else{ //创建键值失败 throw Exception("增加ODBC数据源失败"); } 找到或创建Software\ODBC\ODBC.INI\MyAccess,写入DSN配置信息 if (spReg->OpenKey("Software\\ODBC\\ODBC.INI\\" + DSN, true)) { spReg->WriteString("DBQ", strMDBFile);//数据库目录,连接您的数据库 spReg->WriteString("Description", strDesc);//数据源描述 char buf[MAX_PATH]; ::GetSystemDirectory(buf, MAX_PATH); spReg->WriteString("Driver", AnsiString(buf) + "\\odbcjt32.dll" );//驱动程序DLL文件 spReg->WriteInteger("DriverId", 25 ); //驱动程序标识 spReg->WriteString("FIL", "Ms Access;" ); //Filter依据 spReg->WriteInteger("SafeTransaction", 0 ); //支持的事务操作数目 spReg->WriteString("UID", strLoginUser);//用户名称 spReg->WriteString("PWD", strPassword);//用户密码 BYTE bData = 0; spReg->WriteBinaryData("Exclusive", &bData, 1); //非独占方式 spReg->WriteBinaryData("ReadOnly", &bData, 1 ); //非只读方式 spReg->CloseKey(); }else{ throw("增加ODBC数据源失败"); }; //找到或创建Software\ODBC\ODBC.INI\MyAccess\Engines\Jet //写入DSN数据库引擎配置信息 if (spReg->OpenKey("Software\\ODBC\\ODBC.INI\\" + DSN + "\\Engines\\Jet", true)) { spReg->WriteString( "ImplicitCommitSync", "Yes"); spReg->WriteInteger("MaxBufferSize", 2048 );//缓冲区大小 spReg->WriteInteger("PageTimeout", 10 );//页超时 spReg->WriteInteger("Threads", 3 );//支持的线程数目 spReg->WriteString("UserCommitSync", "Yes"); spReg->CloseKey(); }else{ throw("增加ODBC数据源失败"); } return true; }

|