In this tutorial we will demonstrate how to use basic data access with a SQL database. This example will be using the Northwind sample database that will be included with the source of the project.

Databases are great, organized ways to store large amounts of data. In some scenarios you may find that you want to access this data in the database and perhaps maybe even display it on a web page. Let’s go ahead and get some data from a database using SQL and simple ASP.NET. To begin, create a new ASP.NET Empty Web Site and:

  1. Right click the project in your solution explorer.
  2. Select add ASP.NET folder.
  3. Select the App_Data folder.
  4. Right click the App_Data folder.
  5. Select add existing item.
  6. Select NORTHWND.mdf.

Once we have added our sample database to the project, we next need to create the page that we will use to display the data on. Go ahead and add a new page named ‘Default.aspx’. Open Default.aspx up to design mode and:

  1. Drag and drop a sqldatasource control onto the page.
  2. Expand the tasks menu of the sqldatasource.
  3. Click configure data source.
  4. From the drop down menu select NORTHWND.MDF.
  5. Click next.
  6. This page simply asks if you want to save your connection string. Click next and it will be saved with the default name.
  7. Next, we see the page we would use to configure our SQL query. Currently we will be selecting all of the data from the alphabetical list of products table. This is okay so just hit next.
  8. Click finish.

This has generated a SQL query for us to select all data from a specific table, but you can modify the query for your needs. Next, we will need something to use to display the data. Go ahead and drag a gridview control onto the the page and:

  1. Expand the tasks menu of the gridview.
  2. From the drop down list choose ‘SqlDataSource1′.

Notice that the grid view expands to accommodate all of the columns from the table we are selecting data from. Go ahead and load up the web site to test this out. You should see a large list of products on your page which we successfully selected from our database using SQL!

Download Source Files