Home | Overview | How Do I | FAQ | Sample | Tutorial | ODBC Driver List
This article applies to the MFC ODBC classes. For information about the MFC DAO classes, see the article Data Access Objects (DAO).
This article explains:
Connecting to a data source means establishing communications with a DBMS in order to access the data. When you connect to a data source from an application through an ODBC driver, the driver makes the connection for you, either locally or across a network.
You can connect to any data source for which you have an ODBC driver. Users of your application must also have the same ODBC driver for their data source. For more information about redistributing ODBC drivers, see Redistributing ODBC Components to Your Customers in the article ODBC.
ODBC Administrator is used to configure your data sources. You can also use ODBC Administrator after installation to add or remove data sources. When you create applications, you can either direct your users to the ODBC Administrator to let them add data sources, or you can build this functionality into your application by making direct ODBC installation calls. For more information, see the Setup DLL Function Reference chapter in the ODBC Programmer’s Reference, the article ODBC Administrator, and the online ODBC API Reference help system.
If multiple users are connected to a data source, they can change data while you are manipulating it in your recordsets. Similarly, your changes may affect other users’ recordsets. For more information about how your updates affect other users, and how their updates affect you, see the articles Recordset: How Recordsets Update Records (ODBC) and Transaction (ODBC).
ClassWizard uses a default connection string to establish a connection to a data source. You use this connection to view tables and columns while you are developing your application. However, this default connection string may not be appropriate for your users’ connections to the data source through your application. For example, their data source and the path to its location may be different from the one used in developing your application. In that case, you should re-implement the CRecordset::GetDefaultConnect member function in a more generic fashion and discard ClassWizard’s implementation. For example, use one of the following approaches:
CString CApp1Set::GetDefaultConnect()
{
return "ODBC;DSN=afx;UID=sa;PWD=Fred;";
}
To generalize it, rewrite GetDefaultConnect so that it returns one of the following values:
// Most general case. User must select data source and
// supply user and password
return "ODBC;";
// User and password required
return "ODBC;DSN=mydb;";
// Password required
return "ODBC;DSN=mydb;UID=sa;";
// On most systems, will connect to server w/o any queries to user
return "ODBC;DSN=mydb;UID=sa;PWD=Fred;";
To connect to a specific data source, your data source must already have been configured with ODBC Administrator.
To connect to a specific data source
For more information about how to specify the data source if it is something other than the one you specified with ClassWizard, see CDatabase::OpenEx or CDatabase::Open in the Class Library Reference.
You must close any open recordsets before calling the Close member function of CDatabase. In recordsets associated with the CDatabase object you want to close, any pending AddNew or Edit statements are canceled, and all pending transactions are rolled back.
To disconnect from a data source
You can reuse a CDatabase object after disconnecting from it, whether you use it to reconnect to the same data source or to connect to a different data source.
See Also Data Source: Determining the Schema of the Data Source (ODBC) CRecordset