Basic Data Access with a SQL Database and ASP.NET 4.0
See more tutorials in Database. This post has Comments Off on Basic Data Access with a SQL Database and ASP.NET 4.0.
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:
- Right click the project in your solution explorer.
- Select add ASP.NET folder.
- Select the App_Data folder.
- Right click the App_Data folder.
- Select add existing item.
- 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:
- Drag and drop a sqldatasource control onto the page.
- Expand the tasks menu of the sqldatasource.
- Click configure data source.
- From the drop down menu select NORTHWND.MDF.
- Click next.
- This page simply asks if you want to save your connection string. Click next and it will be saved with the default name.
- 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.
- 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:
- Expand the tasks menu of the gridview.
- 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!