Access is a desktop database system integrated in Microsoft Office. In some small-scale database applications, it is more convenient to use Access.
When using VS and Access programming, there are still some problems to pay attention to, here are records for future reference. I use VS2015, C++ language, Access2016 database.
1 Environment settings
To use ADO to access the database in the VS application, you need to add the following statement at the end of stdafx.h:
#import "C:\Program Files (x86)\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
2 connection string
For the current version of Access, the connection string is:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DBPath
The DBPath in the above statement should be replaced with the real path of your own database file, for example
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\MyDBApp\\Data\\Test.accdb
In fact, this string can be constructed using VS tools. The method is:
In VS2015, menu <Tools>——<Connect to Database>, pop up the <Select Data Source> window, select <Microsoft Access Database File>, click <Continue> button.
On the <Add Connection> page that pops up, browse for the database file name item to find your database file, and then click the <Test Connection> button. Normally, a prompt "Test Connection Successful" should pop up. If the test fails, you need to check other problems.
After the test connection is successful, click the <Advanced> button, and the <Advanced Properties> page will pop up. The bottom line shows the connection string, as shown in the figure below. Copy it and put it into the program for direct use.
3 Connect to the database
Refer to the following statement to correctly connect to the database
Header file declaration:
_ConnectionPtr m_pConnection;//连接access数据库的链接对象
_RecordsetPtr m_pRecordset;//结果集对象
Implementation file:
bool CMyDB::OpenConn(char* dbname)
{
try {
CoInitialize(NULL);
if (dbname != NULL)
sprintf_s(g_pDBName, "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%s", dbname);
m_pConnection.CreateInstance("ADODB.Connection"); //创建连接对象实例
_bstr_t strConnect = g_pDBName;
m_pConnection->Open(strConnect, "", "", adModeUnknown); //打开数据库
}
catch (_com_error e) {
AfxMessageBox(e.Description());
return false;
}
return true;
}
4 Program configuration
It cannot be configured with X64. If it is configured with X64, it will fail to connect to the database, and it will prompt: "The provider cannot be found, and the program may not be installed correctly". It can be configured with WIN32 instead.
Attached: A few memo tips
1) Memo SQL statement
The following are sentences that are often used but can't be remembered, write them down here.
Statement to get the number of the record just added
select @@identity
2) Processing of single quotes (') in the string content
When the string contains a single quotation mark ('), an error will occur when it is used as a query condition or when inserting a value. In this case, replace the single quotation mark (') in the string with double single quotation marks ('').