⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vcg13.htm

📁 Visual C++与数据库的连接经典实例
💻 HTM
📖 第 1 页 / 共 5 页
字号:

<BR>

<BR>

<LI>If a CDaoRecordset is created that doesn't have a pointer to a CDaoDatabase object supplied to it, MFC will create a temporary CDaoDatabase DAO object. MFC will also create a temporary Workspace object. The database and its workspace may be accessed using the CDaoRecordset data member.

<BR>

<BR>

</UL>

<P>The CDaoWorkspace class provides a host of other database operations, such as repairing corrupted databases or compacting databases. Table 13.2 lists the members of the CDaoWorkspace class.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 13.2. The CDaoWorkspace class members.</B></FONT></CENTER>

<BR>



<CENTER><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Member</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Description</I>

</FONT>

<TR>

<TD COLSPAN=2 ALIGN=center  VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Data Members</I>

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

m_pDAOWorkspace

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

A pointer to the basic underlying DAO workspace object.

</FONT>

<TR>

<TD COLSPAN=2 ALIGN=center  VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Construction</I>

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

CDaoWorkspace()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

A constructor for the CDaoWorkspace object. After creating the CDaoWorkspace object, you must call either Create() or Open().

</FONT>

<TR>

<TD COLSPAN=2 ALIGN=center  VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Attributes</I>

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetIsolateODBCTrans()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns a value used to indicate whether multiple transactions that involve the same ODBC data source are isolated.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetName()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns the user-defined name for the Workspace object.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetUserName()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns the user name (workspace owner) specified when the workspace was created.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

IsOpen()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns a nonzero value if the workspace is open.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SetIsolateODBCTrans()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Specifies whether multiple transactions that involve the same ODBC data source will be isolated.

</FONT>

<TR>

<TD COLSPAN=2 ALIGN=center  VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Operations</I>

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Append()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Appends a newly created workspace to the database engine's Workspaces collection.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

BeginTrans()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Begins a new transaction. The new transaction will apply to all databases open in the workspace.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Close()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Closes the workspace and all of the objects it contains. If there are any pending transactions, they are rolled back (discarded).

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

CommitTrans()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Completes the current transaction and saves the specified changes.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

CompactDatabase()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Compacts (or duplicate) the database.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Create()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Creates a new DAO Workspace object.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetDatabaseCount()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns a value that indicates the number of DAO Database objects in the workspace's Databases collection.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetDatabaseInfo()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns information about a specified DAO database defined in the workspace's Databases collection.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetWorkspaceCount()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns the number of DAO Workspace objects in the database engine's Workspaces collection.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetWorkspaceInfo()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns information about a specified DAO workspace defined in the database engine's Workspaces collection.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Open()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Explicitly opens a workspace object associated with DAO's default workspace.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

RepairDatabase()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Attempts to repair a damaged database.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Rollback()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Ends the current transaction. Any changes won't be saved.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Idle()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Allows the database engine to perform background tasks.

</FONT>

<TR>

<TD COLSPAN=2 ALIGN=center  VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Database Engine Properties</I>

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetVersion()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns a string that contains the version of the database engine associated with the workspace.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetIniPath()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns the location of the Microsoft Jet database engine's initialization settings in the Windows registry.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

GetLoginTimeout()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Returns the number of seconds before an error occurs when the user attempts to log in to an ODBC database.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SetDefaultPassword()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Sets the password that the database engine uses when a workspace object is created without a specific password.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SetDefaultUser()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Sets the user name that the database engine uses when a workspace object is created without a specific user name.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

SetIniPath()

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Sets the location of the Microsoft Jet database engine's initialization settings in the Windows registry.</FONT>

</TABLE></CENTER><BR>

<A NAME="E69E196"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B><I>CDaoDatabase</I></B></FONT></CENTER></H4>

<BR>

<P>The CDaoDatabase object is used to represent the connection to a database (from the CDaoWorkspace object) through which you can operate on the database's data.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>For more information on which database formats are supported, refer to the CDaoWorkspace::GetName() member function in the Visual C++ Books Online.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>More than one CDaoDatabase object may be active at a given time in a given CDaoWorkspace object. The workspace maintains a collection of open database objects called the Databases collection. For more information, see the section &quot;CDaoWorkspace.&quot;

<BR>

<P>You can create CDaoDatabase database objects implicitly when you create Recordset objects. You also can create a CDaoDatabase object explicitly. If you want to use an existing database explicitly with CDaoDatabase, you can do one of the following:

<BR>

<UL>

<LI>You can construct a CDaoDatabase object and pass it a pointer to an already open CDaoWorkspace object.

<BR>

<BR>

<LI>You can create a CDaoDatabase object without specifying the workspace. In this situation, MFC will create a new, temporary CDaoWorkspace object.

<BR>

<BR>

</UL>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>It's possible to create a new Microsoft Access (.MDB) database. First, construct a CDaoDatabase object, and then call the CDaoDatabase object's Create() member function. It's not proper to call Open() after Create() in this situation.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>When using CDaoDatabase, you may open an existing database by creating the CDaoDatabase object and then calling the Open() member function.

<BR>

<P>Each of these techniques will append a CDaoDatabase object to your CDaoWorkspace object and then open a connection to the database's data. You then will create CDaoRecordset, CDaoTableDef, or CDaoQueryDef objects to utilize the database. When CDaoRecordset, CDaoTableDef, or CDaoQueryDef objects are created, you pass to the constructors for these objects a pointer to your CDaoDatabase object. When the application has finished working with the connection, a call to the Close() member function closes the connection. You then must close any recordsets that you haven't already closed and destroy the CDaoDatabase object.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>If you already know ODBC, things might be starting to look familiar since ODBC and DAO share a common model. The following paragraphs discuss transaction processing&#151;which, except for class names, works like ODBC's transaction processing.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>DAO provides transaction processing at the workspace level. See the CDaoWorkspace::BeginTrans, CDaoWorkspace::CommitTrans, and CDaoWorkspace::Rollback functions in the Visual C++ Books Online. For additional information about transactions, search for the article &quot;DAO Workspace: Managing Transactions&quot; in the <I>Programming with MFC</I> title in Books Online.

<BR>

<P>As I mentioned earlier, DAO can interface with ODBC data sources. Microsoft recommends that you attach your ODBC data sources to a Microsoft Access (.MDB) database as external tables. For further information about this technique, see the article &quot;DAO External: Working with External Data Sources&quot; in the <I>Programming with MFC</I> title in Books Online.

<BR>

<P>With DAO, each database will maintain its own collections of Tabledef, Querydef, Recordset, and Relation objects. The DAO CDaoDatabase class will supply the necessary member functions to allow your application to manipulate these objects. These objects are stored in DAO itself, not in the DAO MFC class objects. There are also MFC classes for Tabledef, Querydef, and Recordset objects. There is no MFC class for Relation objects at this time.

<BR>

<P>Table 13.3 lists the members of the CDaoDatabase class.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -