Log out of m_AdoConn.ExitConnect () after the insert operation. If the database is closed, the following error occurs: Unhandledexceptionin (KERNEL32.DLL): 0xE06D7363: MicrosoftCExce indicates that the database was not closed the last time it was started. After it is disabled, the problem is solved. Database insert record method CStringsql; ADOConnm_AdoConn; m
Exit m_AdoConn.ExitConnect () after the insert operation. When the database is closed, the following error occurs: Unhandled exception in (KERNEL32.DLL): 0xE06D7363: Microsoft C Exce found that it was not closed after the database was last opened. After it is disabled, the problem is solved. Database insert record method CString SQL; ADOConn m _ AdoConn; m
Insert and exit m_AdoConn.ExitConnect (); database. When the interface is closed, an error is returned:
Unhandled exception in (KERNEL32.DLL): 0xE06D7363: Microsoft C ++ Exce
The last time the database was opened, it was not closed. After it is disabled, the problem is solved.
Database insertionRecordMethod
CString SQL;
ADOConnM_ AdoConn;
M_ AdoConn. OnInitADOConn ();
SQL.MAt (_ bstr_t ("select * froMCustoM"));
_ RecordsetPtrM_PRecordset;
M_PRecordset=M_ AdoConn. GetRecordset (_ bstr_t) SQL );
Try
{
M_PRecordset->AddNew();
M_PRecordset-> PutCollect ("User ID", _ variant_t ("004 "));
M_PRecordset-> PutCollect ("username", _ variant_t ("Li Gang "));
M_PRecordset-> PutCollect ("password", _ variant_t ("123456 "));
M_PRecordset-> Update ();
M_AdoConn.ExitConnect ();
}
Catch (_ coM_ Error & e)
{
AfxMEssageBox (e. Description ()
)
Vc and access insertRecord
1. Overview of ADO Data Access
ADO is designed by Microsof's powerful data access example ole db and is an easy-to-use application layer interface. ADO enables you to write applications to access and operate data on database servers through the OLE. DB Provider. The main advantages of ADO are ease of use, fast speed, low memory consumption, and small disk relics. ADO uses the least network traffic in key application solutions, and uses the least number of layers between the front end and the data source. All these are designed to provide lightweight, high-performance interfaces. ADO is a familiar metaphor for OLE Automation interface.
Ole db is a set of "Component Object Model" (COM) interfaces. It is a new low-level database interface that encapsulates ODBC functions, access data stored in different information sources in a unified manner. Ole db is the technical basis for Microsoft UDA (Universal Data Access) policies. Ole db provides high-performance access to any data source, including relational and non-relational databases, e-mail and file systems, text and graphics, and custom business objects. That is to say, ole db is not limited to ISAM, Jet, or even relational data sources. It can process any type of data without considering their format and storage methods. In practical applications, this diversity means that you can access data that resides in an Excel spreadsheet, text file, email/directory service, or even a mail server, such as Microsoft Exchange. However, the ole db Application Programming Interface is designed to provide the best functions for various applications and does not meet the simplification requirements. The API you need should be a bridge between applications and ole db, Which is ActiveX Data Objects (ADO ).
2. Use ADO in VC (the development steps are as follows:
1. Introduce the ADO Library File
Before using ADO, you must use the direct import symbol # import in the stdafx. h header file of the project to introduce the ADO library file so that the compiler can compile it correctly. The Code is as follows:
Import ADO library files with # import
# Import "c: \ program files \ common files \ system \ ado \ msado15.dll" no_namespaces rename ("EOF" adoEOF ")
This statement declares that ADO is used in the project, but the namespace of ADO is not used. To avoid constant conflicts, change the constant EOF to adoEOF. Now you can use the ADO interface without adding another header file.
2. initialize the OLE/COM library Environment
Note that the ADO library is a group of COM dynamic libraries, which means that the application must initialize the OLE/COM library environment before calling ADO. In an MFC application, a better method is to initialize the OLE/COM library environment in the InitInstance member function of the main class of the application.
BOOL CMyAdoTestApp: InitInstance ()
{
If (! AfxOleInit () // This is the initialization COM Library
{
AfxMessageBox ("OLE initialization error !");
Return FALSE;
}
......
}
3. Introduction to ADO Interfaces
The ADO library contains three basic interfaces: _ ConnectionPtr, _ CommandPtr, and _ RecordsetPtr.
_ ConnectionPtr interface returnsRecordSet or a null pointer. It is usually used to create a data connection or execute an SQL statement that does not return any results, such as a stored procedure. Use the _ ConnectionPtr interface to returnRecordSet is not a good way to use. ForRecordThe _ RecordserPtr operation is usually used. When you use _ ConnectionPtr, You need to obtainRecordThe number of entries must traverse allRecordBut it is not required when _ RecordserPtr is used.
_ CommandPtr interface returnsRecordSet. It provides a simple way to execute the returnRecordSet of stored procedures and SQL statements. When using the _ CommandPtr interface, you can use the global _ ConnectionPtr interface, or you can directly use the connection string in the _ CommandPtr interface. If you only perform one or several data access operations, the latter is a good choice. However, if you want to frequently access the databaseRecordSet, you should use the global _ ConnectionPtr interface to create a data connection, and then use the _ CommandPtr interface to execute stored procedures and SQL statements.
_ RecordsetPtr isRecordSet object. Compared with the preceding two objectsRecordSet provides more control functions, suchRecordLock, cursor control, etc. Like the _ CommandPtr interface, it does not have to use a created data connection. You can use a connection string to replace the connection pointer with the connection Member variable assigned to _ RecordsetPtr, create a data connection by yourself. If you want to use multipleRecordThe best way is to use the global _ ConnectionPtr interface that has created a data connection like the Command object.
And then use _ RecordsetPtr to execute stored procedures and SQL statements.
4. Use the _ ConnectionPtr Interface
_ ConnectionPtr is mainly a connection interface that gets a connection to the database. Its connection string can be directly written by itself or directed to an odbc dsn ..
_ ConnectionPtr pConn;
If (FAILED (pConn. CreateInstance ("ADODB. Connection ")))
{
AfxMessageBox ("Create Instance failed! ");
Return;
}
CString strSRC;
StrSRC = "Driver = SQL Server; Server = ";
StrSRC + = "suppersoft ";
StrSRC + = "; Database = ";
StrSRC + = "mydb ";
StrSRC + = "; UID = SA; PWD = ";
CString strSQL = "Insert into student (no, name, sex, address) values (3, 'aaa', 'male', 'beijing ')";
_ Variant_t varSRC (strSRC );
_ Variant_t varSQL (strSQL );
_ Bstr_t bstrSRC (strSRC );
If (FAILED (pConn-> Open (bstrSRC, "", "",-1 )))
{
AfxMessageBox ("Can not open Database! ");
PConn. Release ();
Return;
}
COleVariant vtOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR );
PConn-> Execute (_ bstr_t (strSQL), & vtOptional,-1 );
PConn. Release ();
AfxMessageBox ("OK! ");
5. Use the _ RecordsetPtr interface (taking connecting to SQL Server as an example)
_ RecordsetPtr pPtr;
If (FAILED (pPtr. CreateInstance ("ADODB. Recordset ")))
{
AfxMessageBox ("Create Instance failed! ");
Return FALSE;
}
CString strSRC;
StrSRC = "Driver = SQL Server; Server = ";
StrSRC + = "210.46.141.145 ";
StrSRC + = "; Database = ";
StrSRC + = "mydb ";
StrSRC + = "; UID = sa; PWD = ";
StrSRC + = "sa ";
CString strSQL = "select id, name, gender, address from personal ";
_ Variant_t varSRC (strSRC );
_ Variant_t varSQL (strSQL );
If (FAILED (pPtr-> Open (varSQL, varSRC, adOpenStatic, adLockOptimistic, adshorttext )))
{
AfxMessageBox ("Open table failed! ");
PPtr. Release ();
Return FALSE;
}
While (! PPtr-> GetadoEOF ())
{
_ Variant_t varNo;
_ Variant_t varName;
_ Variant_t varSex;
_ Variant_t varAddress;
VarNo = pPtr-> GetCollect ("id ");
VarName = pPtr-> GetCollect ("name ");
VarSex = pPtr-> GetCollect ("gender ");
VarAddress = pPtr-> GetCollect ("address ");
CString strNo = (char *) _ bstr_t (varNo );
CString strName = (char *) _ bstr_t (varName );
CString strSex = (char *) _ bstr_t (varSex );
CString strAddress = (char *) _ bstr_t (varAddress );
StrNo. TrimRight ();
StrName. TrimRight ();
StrSex. TrimRight ();
StrAddress. TrimRight ();
Int nCount = m_list.GetItemCount ();
Int nItem = m_list.InsertItem (nCount, _ T (""));
M_list.SetItemText (nItem, 0, strNo );
M_list.SetItemText (nItem, 1, strName );
M_list.SetItemText (nItem, 2, strSex );
M_list.SetItemText (nItem, 3, strAddress );
PPtr-> MoveNext ();
}
PPtr-> Close ();
PPtr. Release ();
6. Use the _ CommandPtr Interface
_ CommandPtr interface returns A Recordset object and provides moreRecordSet control function. The following code shows how to use the _ CommandPtr interface:
Code 11: Use the _ CommandPtr interface to obtain data
_ CommandPtr pCommand;
_ RecordsetPtr pRs;
PCommand. CreateInstance (_ uuidof (Command ));
PCommand-> ActiveConnection = pConn;
PCommand-> CommandText = "select * from student ";
PCommand-> CommandType = ad1_text;
PCommand-> Parameters-> Refresh ();
PRs = pCommand-> Execute (NULL, NULL, adCmdUnknown );
_ Variant_t varValue = pRs-> GetCollect ("name ");
CString strValue = (char *) _ bstr_t (varValue );
7,AboutData type conversion because COM objects are cross-platform, it uses a common method to process various types of data,
Therefore, the Cstring class is incompatible with the COM Object. We need a set of APIs to convert data of the COM Object and C ++ type. _ Vatiant_t and _ bstr_t are two types of objects. They provide common methods to convert data of the COM Object and C ++ type.