VC ADO Dynamic Create Access Database
ADO technology has now become the mainstream technology for connecting the database. Here I will show how to use ADO to dynamically create an Access database.
To use ADO, you must introduce Microsoft's two dynamic connection libraries Msadox.dll and Msado15.dll:
#pragma Warning (Disable: 4146) #import "c: / program files / common files / system / ado / msadox.dll" #import "c: / program files / common files / system / ado / msado15.dll" no_namespace rename ("Eof", "endoffile" # Pragma Warning (Default: 4146)
Add the above code to the stdafx.h file, because the ADO is a COM component, so I will initialize the COM environment before using ADO:
Coinitialize (NULL);
Below is an example of a SQL statement that creates a table in the Access database:
Create Table Test (nametext (20) with compression not null,
ScoreDecimal (12, 4) Not Null Default 0,
ID Smallint Not Null Default 0,
BIRTHDAY DATE,
SEX Char (1),
constraint ck_ch_sex check (Sex IN ('N', 'V')),
constraint pk_id primary key (id)
);
Use Adox :: CatalogPTR to create an MDB file:
HRESULT HR = S_OK;
// set activeConnection of catalog to this stringcstring strincnn (_t ("provider = microsoft.jet.Oledb.4.0; data source = d: //test.mdb"))));
Try {Adox :: _ CatalogPtr M_PCATALOG = NULL; HR = m_pcatalog.createInstance (__ uuidof (adox :: catalog);
Failed (HR))
{
_com_issue_error (hr);
}
ELSE
{M_pcatalog-> create (_BSTR_T (STRCNN)); // CREATE MDB
}}} catch (_COM_ERROR & E) { // Notify the user of errors if any. AFXMESSAGEBOX (_T ("error"));}
Create a connection object Open the MDB file just established:
_CONNECTIONPTR g_pconn;
g_pconn.createinstance (__ uuidof (connection));
g_pconn-> open ("provider = microsoft.jet.Oledb.4.0; data source = d: //test.mdb;", "," ", adConnectunSpecified;
The following functions are used to perform SQL statements in the SQL file:
// function name: Runsqlscript // DESCRIPTION : Perform SQL scripts, peckermen@163.com, 2003-09-15 // Return Type: BOOL Success Return true // argument: LPCSTR SQLScriptFile SQL Script file name // argument : CHAR SEPARATOR SQL segment symbol, default ';' // argument: char remark SQL Note symbol, default '-'bool runsqlscript (lpcstr sqlscriptfile, char separator ='; ', char remark =' - ' ) {BOOL BRET = false; cfilefind Finder; cstract errlong; if (Finder.FindFile (SqlscriptFile) == true) {CFile FSQL; tchar * buffer, * psql, * p; FSQL.Open (Sqlscriptfile, CFILE & :: ModeRead; UINT NFILENGTH = fsql.getlength (); BUFFER = (tchar *) Malloc (nfilelength 1) * sizeof (tchar)); _tcsnset (buffer, tchar ('/ 0'), NfileLength 1); Uint nbytesread = fsql.read (buffer, nfilelength); // read the contents of the SQL file into the memory buffer FSQL.Close ();
P = psql = buffer; Bool brunok = false; Bool binnote = false; Bool bskip = false; cstring strsql; tchar ch; rlog = _t ("); WHILE (p <(buffer NfileLength)) { // Judgment is a note line IF (binnote) {IF (* p == tchar ('/ x0a')) binnote = false;} ELSE {IF ((* p == remark) && (* (p 1) == remark) { binnote = true; p ; } ELSE { // Judgment is whether it is a SQL statement end flag if (* p == separator) {STRSQL = _t ("); BSKIP = false; WHILE (PSQL
TRY { g_pconn-> cursorLocation = aduseclient; g_pconn-> execute (_BSTR_T (LPCTSTSTSTSTSTSQL), & VRecords, adexecutenorecords; m_nrecordsaffected = VRecords.ival; brunok = true; } catch (_COM_ERROR & E) { brunok = false;} (brunok) errlog = errlog _t ("/ n-- successed! / n"); ELSE {{ errlog = errlog _t ("/ n - failed! / n"); break; } } } } p ; } free (buffer); BRET = Brunok;} Return Bret;}
Call RunSQLScript to create a table in the Access database:
Runsqlscript (_T ("D: //test.sql")); turn off database connection:
g_pconn-> close ();
:: Couninitialize ();
The above code demonstrates how to use ADO to dynamically create an Access database according to SQL files, welcome to correct.