Udostępnij za pośrednictwem


How to: Connect to an ODBC Database Using the SqlDataSource Control

You can use the SqlDataSource control to connect to any ODBC–compliant data source. You connect the control to an ODBC data source by specifying the appropriate ODBC driver in the connection string along with relevant server and authentication information.

To use the SqlDataSource control to connect to an ODBC database

  1. Open the page from which you want to connect to an ODBC database.

  2. Switch to Design view.

  3. From the Data group on the Toolbox, drag a SqlDataSource control onto the page.

  4. If the smart tag panel is not displayed, right-click the control and then click Show Smart Tag.

  5. In the SqlDataSource Tasks list, click Configure Data Source.

    The Configure Data Source wizard is displayed.

  6. In the Configure Data Source dialog box, click New Connection.

  7. If the Add Connection dialog box is displayed, click Change to display the Change Data Source dialog box.

  8. In the Change Data Source dialog box, click Microsoft ODBC Data Source in the Data source list, and then click OK.

    The Add Connection dialog box is displayed.

  9. If you have an existing ODBC data source, click Use user or system data source name and select an existing ODBC data source from the list.

  10. If you do not have an existing ODBC data source, click Use connection string and then type the connection string or click Build to display the Select Data Source dialog box where you can build an ODBC data source name (DSN).

  11. If necessary, enter the user name and password required to connect to the database.

  12. Click Test connection to verify the connection to the ODBC data source, and then close the Add Connection dialog box to return to the Configure Data Source wizard.

  13. Click Next, and then click Next again to have the connection string information saved as part of the Web.config file under the name you provide.

  14. In the Configure the Select Statement pane, if you want to use the wizard to create a SQL query, click Specify columns from a table or view and use the options in the pane to configure your query.

    Note

    To have the wizard generate Insert, Update, and Delete statements based on the Select statement you are creating, click Advanced.

  15. If you want to use the query builder or write your Select query, click Specify a custom SQL statement or stored procedure, click Next, and then write your SQL statement. Click the UPDATE, INSERT, or DELETE tabs to create update statements.

  16. Click Next.

  17. In the Test Query pane, click Test Query to determine whether the query returns the results you want, and then click Finish.

See Also

Tasks

Walkthrough: Basic Data Access in Web Pages

Concepts

Data Source Controls Overview

Binding to Data Using a Data Source Control