ODBC access examples

This topic includes the following sections:

 

The following examples connect to the sample Synergy database provided with the xfODBC installation. These examples were created using Visual Studio 2012 and ODBC Test 2.7. Procedures may differ for other versions.

Note

Before you start, follow the steps in Using the Sample Database As a Tutorial to prepare the sample database for ODBC access by generating a system catalog, and creating a connect file, DSN, and user. The DSN you create for the sample database should specify the connect file. If you initialized users and groups, you can use the default user DBADMIN.

For more examples, see the following Synergex KnowledgeBase articles:

Using ODBC Test to test a query

ODBC Test (Odbcte32.exe) is an ODBC‑enabled application distributed by Microsoft. It’s useful for testing your xfODBC setup and for testing SQL statements. If a query works with ODBC Test, the query and the setup (DSN, connect file, system catalog, data files, environment variable settings, and so forth) are working. If you find that the same setup and SQL statement fail with another ODBC‑enabled application, chances are the problem lies with the application (for example, with the way it generates SQL).

As with the other examples in this section, we’ll access the sample Synergy database.

1. Open ODBC Test.
2. Select Full Connect from the Conn menu.
3. In the Data Source field of the Full Connect dialog box, select the DSN you created for the sample Synergy database, enter the user name and password for the DSN (optional at this point), set the ODBC Behavior field to ODBC 2.0, and then click OK. (Leave the Cursor Library field set to Default.) (The user you log in as must have read privileges to complete this example.)
4. If the xfODBC Info window opens, enter the user name, password, or connect file—whatever information is missing—and click OK. This window opens if any one of these is not stored in the user DSN you selected and was not entered in the Full Connect dialog box.

If xfODBC isn’t able to connect to the sample database, you’ll get an error message (“Connection failed...”) that should give you some idea about the problem. If you are unable to solve the problem from the information provided by the error message, see Troubleshooting data access.

5. If ODBC Test is able to connect to the sample database, a window with two panes will open. Enter the following SQL statement in the upper pane (see figure 1):
SELECT in_latin FROM plants 
    WHERE in_zone = 3 
        AND in_shape = 'tree'
6. Select SQLExecDirect from the Stmt menu, and then click OK in the SQLExecDirect window. The results, which should include SQL_SUCCESS=0, are displayed in the lower pane of the window for the connection. (If you get an error, the SQL statement entered in step 5 is probably incorrect.)

1. The results of SQLExecDirect.

The results of SQLExecDirect

7. If step 6 resulted in SQL_SUCCESS=0, select Get Data All from the Results menu. The results are appended to the information in the lower pane.
8. To disconnect, select Full Disconnect from the Conn menu.

There are many other ODBC API calls you can invoke from ODBC Test. See the online help for ODBC Test for information.

 

Adding and using a data connection in Visual Studio

A data connection enables you to access your Synergy data from within Visual Studio. The steps below explain how to add a data connection, which will then be used in the examples Using the Retrieve Data function and Using Query Designer.

1. In Visual Studio, open the Server Explorer (View > Server Explorer) and select Tools > Connect to Database or right‑click the Data Connections node and select Add Connection from the context menu. (Data connections are independent of solutions and projects, so you don’t need to open a solution or project.) The Add Connection window or Choose Data Source window opens.
2. Do one of the following:
3. In the “Use user or system data source name” field of the Add Connection window, select the DSN for the sample database. If the DSN doesn’t specify a user name and password, enter them here.

2. Entering data connection information in the Add Connection window.

Entering data connection information in the Add Connection window

4. Click the Test Connection button at the bottom of the Add Connection window to make sure the connection information is correct. Note that a “Data Source name is missing” error could indicate that the DSN does not specify a connect file. (For a DSN to work with the .NET Framework Data Provider for ODBC, it must specify a connect file.) For other errors (e.g., “Authorization failure”), check the information on the Add Connection window. If it appears to be correct, see Troubleshooting data access.
5. Once you’re able to connect with the Test Connection button, click OK to close the “Test connection succeeded” message, and then click OK in the Add Connection window. The Add Connection window closes, and a node for the data connection is added to the Data Connections branch of Server Explorer.

Using the Retrieve Data function

Once you’ve established a data connection, you can access the Synergy data from Visual Studio in various ways, including the Retrieve Data function, which displays the data for a table or view.

1. In Server Explorer, expand the data connection node added in step 5 of Adding and using a data connection in Visual Studio (above), expand the Tables node under that, and then right‑click one of the tables (CUSTOMERS, ORDERS, PLANTS, or VENDORS).
2. From the context menu, select Retrieve Data. (See figure 3.)

3. Selecting Retrieve Data from the context menu for the CUSTOMERS table.

Selecting Retrieve Data from the context menu for the CUSTOMERS table

A tab with the name of the table is added to the editor pane of Visual Studio. The tab displays all the columns and rows for the table. (See figure 4.)

4. Results of Retrieve Data function for CUSTOMERS table.

Results of Retrieve Data function for CUSTOMERS table

Using Query Designer

Another Visual Studio tool you can use with a data connection is Query Designer, which enables you to enter SQL or use graphical tools to query a relational database.

1. In Server Explorer, right‑click the data connection node added in step 5 of Adding and using a data connection in Visual Studio and select New Query from the context menu. If there is no New Query entry on the context menu, select Refresh from the context menu, and then right‑click again and select New Query.
2. In the Add Table window (figure 5), select the tables you want to query (you can use ctrl+click to select more than one) and click Add. The selected tables are displayed in the Diagram pane, the Criteria pane, and as part of the SQL pane.

5. Selecting tables in the Add Table window.

Selecting tables in the Add Table window

3. Click Close to close the Add Table window.
4. Select columns for the query by selecting columns in the Diagram pane (figure 6). The SQL pane displays the revised query.

6. Selecting columns in the Diagram pane.

Selecting columns in the Diagram pane

5. Select Query Designer > Execute SQL to execute the query. The retrieved columns and rows are displayed in the Results pane.