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 (Name Text (20) with Compression Not Null,
Score Decimal (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 PK_ID PRIMARY Key (ID)
);
Use Adox :: CatalogPTR to create an MDB file:
HRESULT HR = S_OK; // SET ActiveConnection of catalog to this string cstring 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);
IF (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 ("r"));}
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: Execute SQL script, Peckermen@163.com, 2003-09-15 // Return Type: BOOL Success Back True // Argument: LPCSTR SQLScriptFile SQL Script File Name // Argument: Char Separator SQL split symbol, default ';' // argument: char remark SQL Note Symbol, default '-'boolution runsqlscript (lpcstr sqlscriptfile, char setrib =' - ') {bool Bret = false; CFileFind finder; CString ErrLong; if (finder.FindFile (SqlScriptFile) == TRUE) {CFile fSql; TCHAR * buffer, * pSQL, * p; fSql.Open (SqlScriptFile, CFile :: modeRead); UINT nFileLength = fSql.GetLength (); Buffer = (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; errlog = _t ("); while (p <(buffer nfilelength)) {// Decision Yes Note Ring IF (binnote) {if (* p == tchar ('/ x0a')) binnote = false;} else {IF ((* p = = remark) && (* (p 1) == transark) ) {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 ((LPCTSTR) strSQL), & vRecords, adExecuteNoRecords); m_nRecordsAffected = vRecords.iVal; bRunOK = TRUE;} catch (_com_error & e) {bRunOK = FALSE;} if (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 the 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.
Peckermen@163.com