First ASP.NET Database Program
The following ASP.NET database program based on the "pubs" database. If you dont have the pubs database on your SQL server please follow the url.
After you setup the pubs database on your SQL server , you need to make a connectionstring in your we.config file.
In order to making a connection string you have to provide the following information.
- SqlServer Name
- Database Name
After collecting above information , you have to create web.config file like shown below , or copy and paste the below
content in your web.config file and fill the appropriate parameter values.
Here we are going to connect the database and count the number of rows in the publishers table.
The sql command is : SELECT COUNT(*) FROM publishers
In order to retrieve the connection string value from web.config file , we code like the following :
After we retrieve the connection string , we will create a command object for execute the sql and read the result using SqlDataReader.
Finally we retrieve the result in a string and show it in a Label control.
Click the following links to see full source code