Script-based Database Interaction for Your ASP.NET Website | 2 | WebReference

Script-based Database Interaction for Your ASP.NET Website | 2


[prev]

Script-based Database Interaction for Your ASP.NET Website [con't]

Executing Commands

We know how to connect to a database, but how can we get information out of it? Easy. We need to use the constructor called SQLCommand, which will accept a query that we define:

Our query tells the command object to get all the information from the cd table. To check if our command returned any results, we use the SqlDataReader object. This object gives us a number of options, as shown in the table below.

Item Description
ExecuteReader Executes the query and returns the sqldatareader object
ExecuteNonQuery Executes the query but does not return any results (Mostly used for queries such as UPDATE or DELETE)
ExecuteScalar Executes the query and returns a single value from the first column of the first row

If we just want to read all the data returned by our query, then this is what we will do:

Conclusion

In this article, we saw how ASP.NET interacts with databases by managing data in various scripts and adding, modifying and deleting records.

Original: July 30, 2010


[prev]