MFC ListControl and AccessC + + read mdb file

CDaoDatabase

Open

Prototype:

virtual void Open( 
   LPCTSTR lpszName, 
   BOOL bExclusive = FALSE, 
   BOOL bReadOnly = FALSE, 
   LPCTSTR lpszConnect = _T( 
   "" ) 
);

Open the database file.

  • lpszName: database file name
  • bExclusive: is it exclusive access
  • Bread only: is it read-only
  • lpszConnect: I don't know what it is
    The interpretation of the official document is as follows:
    A string expression is used to open the database. This string combines ODBC connection parameters. You must provide exclusive and read-only parameters to provide the source string. If the database is a Microsoft Jet database (. MDB), the string is empty (""). Default -_ T("") syntax - portability for Unicode and ANSI compilation of your application. (feeling is the problem of translation)

Close

Prototype:

virtual void Close( );

Disconnect the database and close any open recordsets, tabledefs, and querydefs.

CDaoRecordset

Prototype:

class CDaoRecordset : public CObject

Represents a set of records selected from a data source.

When creating an object, you need to provide the address of the database source.

Open

Prototype:

virtual void Open(
   int nOpenType = AFX_DAO_USE_DEFAULT_TYPE,
   LPCTSTR lpszSQL = NULL,
   int nOptions = 0 
);

Get table contents.

  • nOpenType

One of the following values:

  1. Dynamic type recordset of dbOpenDynaset using bidirectional scrolling. This is the default setting.

  2. A recordset of the table type of dbOpenTable that uses two-way scrolling.

  3. Snapshot type recordset of dbOpenSnapshot using two-way scrolling.

  • lpszSQL

A string pointer that contains one of the following operations:

  1. NULL pointer.

  2. The names of one or more tabledefs and querydefs (separated by commas).

  3. SQL SELECT statement (optional) and SQL WHERE or order by clause).

  4. Pass query.

  • nOptions

One or more tabs listed below. The default value is 0. Possible values are as follows:

  1. dbAppendOnly can append only new records (only dynamically typed recordsets). This option means that records can only be appended according to the original meaning. The MFC ODBC database option class has an append option that allows record retrieval and append.

  2. The dbForwardOnly recordset is a forward scrolling snapshot.

  3. dbSeeChanges generates an exception if another user changes the data being edited.

  4. Other users cannot modify or add dbDenyWrite of records.

  5. dbDenyRead other users cannot view records (only recordsets of table type).

  6. dbReadOnly can only view logs; Other users can modify them.

  7. dbInconsistent inconsistent updates (only dynamically typed recordsets are allowed).

  8. Only dbConsistent consistent updates are allowed (only dynamically typed recordsets are allowed).

IsEOF

Prototype:

BOOL IsEOF( ) const;

Judge whether there are records in the current position.

GetFieldCount

Prototype:

short GetFieldCount( );

Retrieve several columns of data in the recordset.

GetFieldValue

Prototype:

virtual void GetFieldValue(
   LPCTSTR lpszName,
   COleVariant& varValue 
);

Retrieves data from a recordset.

  • pszName
    Pointer to the string containing the field name.

  • varValue
    A reference to the COleVariant object to store the value of the field.

MoveNext

Prototype:

void MoveNext( );

Move to the next entry in the recordset.

Close

Prototype:

virtual void Close( );

Close the CDaoRecordset object, open the recordset collection, and remove it from the associated database.

COleVariant

ChangeType

Prototype:

void ChangeType(
   VARTYPE vartype,
   LPVARIANT pSrc = NULL 
);

Convert the types of different values.

  • vartype
    VARTYPE of the COleVariant object.

  • pSrc
    Pointer to the variable object to be converted. If this value is NULL, this COleVariant object is used as a data source pair transformation.

pbstrVal

Used to convert the value of the COleVariant object.

example

//To use vector, you must first import include < vector >

vector<vector<CString>> AccessAuxiliary::AccessAuxiliaryRead(CString lib, CString table, vector<CString> name) {
/*
lib: File name, such as access mdb
table: Table name, such as motor*/

	vector<vector<CString>> svec;  //Storing table data in a two-dimensional container

	svec.reserve(10);  //Preset 10 lines

	CString str = _T("SELECT * FROM ") + table;

	CDaoDatabase db;
	CDaoRecordset RecSet(&db);

	COleVariant var;

	int num = 0;

	var.ChangeType(VT_BSTR, NULL);

	db.Open(lib);

	RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE, str, NULL);

	while (!RecSet.IsEOF())
	{
		svec.push_back(vector<CString>());  //Add a new row, otherwise an index overflow will occur
		svec[num].reserve(10);

		for (short i = 0; i < RecSet.GetFieldCount(); i++)
		{
			RecSet.GetFieldValue(name[i], var);

			svec[num].push_back((LPCTSTR)var.pbstrVal);  //Cast the value of the COleVariant object
		}
		RecSet.MoveNext();
		num++;
	}

	RecSet.Close();
	db.Close();

	svec.shrink_to_fit();

	return svec;  //After obtaining the final data, you can process these data in other functions.
}

Posted by Hagaroo on Sun, 17 Apr 2022 04:20:21 +0930