Share via


Accessing Data with ASP.NET

ASP.NET includes data access tools that make it easier than ever for you to design sites that allow your users to interact with databases through Web pages.

The .NET Framework includes two data providers for accessing enterprise databases: the .NET Framework Data Provider for OLE DB and the .NET Framework Data Provider for SQL Server. This section focuses on accessing SQL Server (version 7.0 or later) databases using the .NET Framework Data Provider for SQL Server, but you can adapt the code examples to other databases with only minor changes.

To access SQL databases from ASP.NET

  1. Create a database connection using the SqlConnection class.

  2. Select a set of records from the database using the SqlDataAdapter class.

  3. Fill a new DataSet using the SqlDataAdapter class.

  4. If you are selecting data from a database for non-interactive display only, it is recommended that you use a read-only, forward-only SqlDataReader (or OleDbDataReader for non-SQL databases) for best performance. When using a SqlDataReader, select the records using a SqlCommand query and create a SqlDataReader that is returned from the SqlCommand object's ExecuteReader method.

    In some cases, such as when you want to sort or filter a set of data, you might also want to create a new DataView based on a DataSet for the desired table.

  5. Bind a server control, such as a DataGrid, to the DataSet, SqlDataReader, or DataView.

The .NET Framework includes three controls that make the display of large amounts of data easier: the Repeater control, the DataList control, and the DataGrid control. These three controls all use similar data-binding procedures, as explained in the sections that follow. For additional examples of how to use these controls, see the Server-Side Data Access and Data Access and Customization sections of the ASP.NET QuickStart.

For a general discussion of database access, see Overview of ADO.NET and its subtopics.

In This Section

  • Accessing Data with ADO.NET
    Describes the ADO.NET architecture and how to use the ADO.NET classes to manage application data and interact with data sources including Microsoft SQL Server, OLE DB data sources, and XML.