Create an .MDB File for Microsoft Access Databases NOTE: For information about how to create .MDB files programmatically using Visual C++ version 4.x or later, please see the following article in the Microsoft Knowledge Base: Q126606 INFO: Accessing CREATE_DB, REPAIR_DB, and COMPACT_DB An .mdb file is always required to configure a Microsoft Access data source either by using the Open Database Connectivity (ODBC) administrator or by configuring the data source programmatically. An .mdb file cannot be created by using the MFC Database Classes or the ODBC application programming interface (API). You can use one of the following methods to have an application configure a data source on your computer: Ship and install the .mdb file along with the application. Make the .mdb file into a user-defined resource. Then, copy the .mdb file at run time. This article discusses the second method, making the .mdb file into a user- defined resource. More Information You can use a user-defined resource to attach miscellaneous data to a .EXE file. If an .mdb file is made into a resource, the resource can be loaded at run time and then written to an .mdb file. The steps required to do this are as follows: Create an .mdb file either by using Microsoft Access or MSQuery. Create the .mdb file as a user-defined resource by adding the following line to your .RC file: mdb_file MDB_RESOURCE filename.mdb where: mdb_file is a name for identifying the resource. MDB_RESOURCE is a name for identifying the type of the resource and can be any user-defined type. filename.mdb is the name of the .mdb file. Load the resource and write it out to an .mdb file. You can use the following function, from anywhere in your application, to create the .mdb file. (A typical place for creating the .mdb file would be the override of the CWinApp::InitInstance() function.) CreateMDBFile() { // Get the instance handle - required for loading the resource HINSTANCE hInst = AfxGetInstanceHandle(); // Load the user-defined resource. HRSRC hmdbFile = ::FindResource(hInst, "mdb_file", "MDB_RESOURCE"); HGLOBAL hRes = ::LoadResource(hInst, hmdbFile); DWORD dwResSize = ::SizeofResource(hInst, hmdbFile); if (hRes != NULL) { UINT FAR* lpnRes = (UINT FAR*)::LockResource(hRes); CString szFileName = "Filename.mdb"; TRY { // Create the .mdb file CFile f( szFileName, CFile::modeCreate | CFile::modeWrite ); // Write the user-defined resource to the .mdb file f.WriteHuge(lpnRes, dwResSize); f.Flush(); } CATCH( CFileException, e ) { #ifdef _DEBUG afxDump << "File could not be opened " << e->m_cause << "\n"; #endif } END_CATCH #ifndef WIN32 //Unlock Resource is obsolete in the Win32 API ::UnlockResource(hRes); #endif ::FreeResource(hRes); } } The one disadvantage of this method is that it increases the size of the .exe file according to the size of the .mdb file. Because even an empty .mdb file is 64K in size, the .exe is increased by at least 64K. One way to reduce the size of the .exe is to store a compressed .mdb file onto the .exe and expand it at run time when storing the resource.
The following code shows how to create a new Microsoft Jet database with the Create method.// BeginCreateDatabaseCpp #import "c:\Program Files\Common Files\system\ado\msadox.dll" no_namespace #import "c:\Program Files\Common Files\system\ado\msado15.dll"#define TESTHR(x) if FAILED(x) _com_issue_error(x);#include "iostream.h" #include "stdio.h" #include "conio.h"//Function declarations void CreateDatabaseX(void);//------------------------------------------------------------ // //Main Function //Purpose: Test Driver //------------------------------------------------------------ // void main() { HRESULT hr = S_OK; hr = ::CoInitialize(NULL); if(SUCCEEDED(hr)) { CreateDatabaseX(); //Wait here for the user to see the output printf("Press any key to continue..."); getch();
::CoUninitialize(); } }//------------------------------------------------------------ // //CreateDatabaseX //Purpose: create a new Jet database with the Create method //------------------------------------------------------------ // void CreateDatabaseX() { HRESULT hr = S_OK; // Define ADOX object pointers. // Initialize pointers on define. // These are in the ADOX:: namespace.
_CatalogPtr m_pCatalog = NULL; //Set ActiveConnection of Catalog to this string _bstr_t strcnn("Provider=Microsoft.JET.OLEDB.4.0;" "Data source = c:\\new.mdb"); try { TESTHR(hr = m_pCatalog.CreateInstance(__uuidof (Catalog))); m_pCatalog->Create(strcnn); } catch(_com_error &e) { // Notify the user of errors if any. _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description());
或者做个界面交互获得userid、password等这不叫不通过odbc
而叫不用手工配置odbc数据源
NOTE: For information about how to create .MDB files programmatically using Visual C++ version 4.x or later, please see the following article in the Microsoft Knowledge Base: Q126606 INFO: Accessing CREATE_DB, REPAIR_DB, and COMPACT_DB
An .mdb file is always required to configure a Microsoft Access data source either by using the Open Database Connectivity (ODBC) administrator or by configuring the data source programmatically. An .mdb file cannot be created by using the MFC Database Classes or the ODBC application programming interface (API). You can use one of the following methods to have an application configure a data source on your computer:
Ship and install the .mdb file along with the application.
Make the .mdb file into a user-defined resource. Then, copy the .mdb file at run time.
This article discusses the second method, making the .mdb file into a user- defined resource. More Information
You can use a user-defined resource to attach miscellaneous data to a .EXE file. If an .mdb file is made into a resource, the resource can be loaded at run time and then written to an .mdb file. The steps required to do this are as follows: Create an .mdb file either by using Microsoft Access or MSQuery.
Create the .mdb file as a user-defined resource by adding the following line to your .RC file:
mdb_file MDB_RESOURCE filename.mdb
where:
mdb_file is a name for identifying the resource.
MDB_RESOURCE is a name for identifying the type of the resource
and can be any user-defined type.
filename.mdb is the name of the .mdb file. Load the resource and write it out to an .mdb file. You can use the following function, from anywhere in your application, to create the .mdb file. (A typical place for creating the .mdb file would be the override of the CWinApp::InitInstance() function.)
CreateMDBFile()
{
// Get the instance handle - required for loading the resource
HINSTANCE hInst = AfxGetInstanceHandle(); // Load the user-defined resource.
HRSRC hmdbFile = ::FindResource(hInst, "mdb_file", "MDB_RESOURCE");
HGLOBAL hRes = ::LoadResource(hInst, hmdbFile);
DWORD dwResSize = ::SizeofResource(hInst, hmdbFile); if (hRes != NULL)
{
UINT FAR* lpnRes = (UINT FAR*)::LockResource(hRes);
CString szFileName = "Filename.mdb"; TRY
{
// Create the .mdb file
CFile f( szFileName, CFile::modeCreate | CFile::modeWrite ); // Write the user-defined resource to the .mdb file
f.WriteHuge(lpnRes, dwResSize);
f.Flush();
}
CATCH( CFileException, e )
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e->m_cause << "\n";
#endif
}
END_CATCH #ifndef WIN32 //Unlock Resource is obsolete in the Win32 API
::UnlockResource(hRes);
#endif
::FreeResource(hRes);
}
} The one disadvantage of this method is that it increases the size of the .exe file according to the size of the .mdb file. Because even an empty .mdb file is 64K in size, the .exe is increased by at least 64K. One way to reduce the size of the .exe is to store a compressed .mdb file onto the .exe and expand it at run time when storing the resource.
#import "c:\Program Files\Common Files\system\ado\msadox.dll" no_namespace
#import "c:\Program Files\Common Files\system\ado\msado15.dll"#define TESTHR(x) if FAILED(x) _com_issue_error(x);#include "iostream.h"
#include "stdio.h"
#include "conio.h"//Function declarations
void CreateDatabaseX(void);//------------------------------------------------------------ //
//Main Function
//Purpose: Test Driver
//------------------------------------------------------------ //
void main()
{
HRESULT hr = S_OK; hr = ::CoInitialize(NULL);
if(SUCCEEDED(hr))
{
CreateDatabaseX(); //Wait here for the user to see the output
printf("Press any key to continue...");
getch();
::CoUninitialize();
}
}//------------------------------------------------------------ //
//CreateDatabaseX
//Purpose: create a new Jet database with the Create method
//------------------------------------------------------------ //
void CreateDatabaseX()
{
HRESULT hr = S_OK; // Define ADOX object pointers.
// Initialize pointers on define.
// These are in the ADOX:: namespace.
_CatalogPtr m_pCatalog = NULL;
//Set ActiveConnection of Catalog to this string
_bstr_t strcnn("Provider=Microsoft.JET.OLEDB.4.0;"
"Data source = c:\\new.mdb");
try
{
TESTHR(hr = m_pCatalog.CreateInstance(__uuidof (Catalog)));
m_pCatalog->Create(strcnn); } catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\n\tSource : %s \n\tdescription : %s \n ",(LPCSTR)bstrSource,(LPCSTR)bstrDescription); } catch(...)
{
cout << "Error occured in include files...."<< endl;
}}
// EndCreateDatabaseCpp
create database ..... sql server语句不就行了。
其它用户数据库的,但也要线打开MASTER数据库