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 into the stdafx.h file, due to ADO is a COM component, so I will initialize the COM environment before using ADO: Coinitialize (null); Example of the SQL statement created 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 MDB files: HRESULT HR = S_OK; // set ActiveConnection of catalog to this Stringcstring strcnn (_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 (_BST R_T (strcnn)); // Create MDB}} }catch (_COM_ERROR & E) { // Notify the user of errors if any. AFXMESSAGEBOX (_T ("error"));} Create a connection Object Open Just established MDB file: _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 function is used to execute SQL statements in the SQL file: // function name: Runsqlscript // DESCRIPTION : Perform SQL scripts, peckermen@163.com, 2003-09-15 // Return Type: BOOL Success Returns True // Argument: LPCSTR SQLScriptFile SQL Script File Name // Argument: CHAR SEPARATOR SQL Segmentation Symbol, Default ';'
// argument: char REMARK SQL Note Symbol, Default '-'boolution Runsqlscript (LPCSTR SQLScriptFile, Char Separator ='; ', CHAR Remark =' - ') {BOOL BRET = FALSE; CFILEFIND FINDER; CSTRING Errlong; IF (Finder.FindFile) == 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; errlog = _t (""); WHILE (P <(Buffer NFileLength)) { // Is it a notes IF (binnote) {IF ( * p == tchar ('/ x0a')) binnote = false; } else {IF ((* p = = remark) && (* (p 1) == remark) { binnote = true; p ; } }ELSE { // Decision whether it is a SQL statement end sign if (* p == separator) {STRSQL = _T (""); BSKIP = false; WHILE (PSQL