Start writing ADO applications.
Before using ADO, we also need to add the following statement, so that the library of ADO into the project
.#import "c:\program files\common files\system\ado\msado15.dll"
no_namespace rename("EOF","adoEOF")
Depending on the setup time of the machine different specific paths may not be the same.
In addition, the following warning message will appear when compiling:
MSADO15.TLH (405): Warning c4146:unary minus operator applied to unsigned type, result still unsigned
MSDN advises us not to bother. If you really don't want to see it, you can add one line of code to the stdafx.h:
#pragma warning (disable:4146)
This warning message will not appear again.
ADO uses COM, so you must initialize COM before using ADO, otherwise it cannot be used. You can initialize with AfxOleInit (), but only once, you can't call this function multiple times, It is recommended that you initialize the application's InitInstance method in the App class.
The above general relevant information is detailed, so I will not elaborate, let's look at how I encapsulated the two classes.
Six, ADO Encapsulation class: Cadoconnection and Cadorecordset
First of course is to connect to the data source, the function connected to the data source is the _cadoconnection connect method, it encapsulates the ADO Connection object CreateInstance and open method:
Let's take a look at how I encapsulated:
BOOL CAdoConnection::Connect(LPCTSTR strConnect, long lOptions)
{
m_strConnect = strConnect;
try
{
///创建 Connection 对象---------------------------
HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");
if (SUCCEEDED(hr))
{
// 连接数据库---------------------------------------------
if (SUCCEEDED(m_pConnection->Open(strConnect, "", "", lOptions)))
{
return TRUE;
}
}
}
catch (_com_error e)
{
TRACE(_T(":( 连接数据库发生错误: %s\n"), e.ErrorMessage());
return FALSE;
}
catch (...)
{
TRACE(_T(":( 连接数据库时发生未知错误:"));
}
return FALSE;
}
Define a Cadoconnection class object such as m_adoconnection before use, for example:
CString strSrcName = "E:\\Access\\datebase.mdb";//假设在e盘有这样一个access的数据库文件
CString strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strSrcName;
m_adoConnection.Connect(LPCSTR(strConnect));
This connects to the data source.
The next step is to open the Recordset, and the following is the encapsulation of its Open method: You can pass a SQL statement directly to the three arguments you want to ignore.
HRESULT Cadorecordset::open (LPCTSTR strSQL,
Long loption,
CursorTypeEnum CursorType,
LockTypeEnum LockType)
{
Try
{
if (m_pconnection = NULL)
{
return-1;
Else if (M_precordset = NULL)
{
M_precordset.createinstance ("ADODB. Recordset ");
}
M_precordset->open (_bstr_t (strSQL),
_variant_t ((idispatch*) m_pconnection->getc Onnection (), true),
CursorType, LockType, loption);
if (m_precordset = NULL)
{
return-1;
Return (m_precordset->adoeof) 0:1;
}
catch (_com_error e)
{
TRACE (_t (":( Error opening recordset:%s\n "), E.errormessage ());
Return-1;
}
}
For example, we can use this:CAdoRecordSet rset;
rset.SetAdoConnection(&(GetDocument()->m_adoConnection));//记得要先指定相应的连接对象,否则会出错.
m_strSQL = "select * from city";//要执行的SQL语句.
rset.Open(m_strSQL);