Create an Access ODBC data source in C ++ Builder

zhaozj2021-02-16  44

Want to use the BDE guide Access database to create an ODBC data source. I didn't see the code written by BCB for a long time, so I have rewritten one. If there is any shortcomings, please correct it. ========jianlinlong@163.net

/ * Function function: Create an Access ODBC Data Source Enter Parameters: DSN -------- The name of the Access ODBC data source to be created strMdbfile --------- Access library files STRDESC -------- Description of this data source strloginuser --------- User name strpassword for the file referred to since Strmdbfile ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ "Sign in StrMdbfile" Password return value: successfully returns true, otherwise you will throw an Exception (this will there be a problem?) Usage: (1) CreateAccessDSN ("jll_access", "C: // Windows // Desktop // StarSo. "); (2) CreateAccessDSN (" JLL_Access2 "," C: //Windows//desktop//starso.mdb "," "," admin "," ok_pwd "); Note: #include #Include

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 spReg (new TRegistry ()); Spreg-> rootkey = HKEY_LOCAL_MACHINE; / / Set root key value is hkey_local_machine // Find Software / ODBC / ODBC.INI / ODBC DATA SOURCES IF (Spreg-> OpenKey ("Software // ODBC // ODBC.ini // ODBC DATA SOURCES ", TRUE) {// Register a DSN Name Spreg-> WritestRing (DSN," Microsoft Access Driver (* .mdb) "); spreg-> closekey ();} else {// creation keys Value failing throw exception ("Add ODBC Data Source Failed");} Find or create Software / ODBC / ODBC.INI / MyAccess, write DSN Configuration Information IF (Spreg-> OpenKey ("Software // ODBC // Odbc.ini // " DSN, TRUE)) {spreg-> WritestRing (" DBQ ", strMDbfile; // Database directory, connect your database spreg-> writeString (" description ", strDesc); // data source description char buf [MAX_PATH]; :: getSystemDirectory (BUF, MAX_PATH); Spreg-> WritestRing ("driver", ANSISTRING (BUF) "//odbcjt32.dll"); // Driver DLL file spreg-> Writeinteger ("driverID" , 25); // Driver logo SPREG-> WRITESTRING ("Fil", "MS Access;"); // filter based on spreg-> Writeinteger ("saFetransaction", 0); // supported transaction number spreg- > WriteString ("UID", strloginuser; // User Name Spreg-> WritestRing ("PWD", strpassword); // User Password Byte BData = 0; Spreg-> WriteBinaryData ("Exclusive", & BData, 1);

转载请注明原文地址:https://www.9cbs.com/read-27925.html

New Post(0)