Querying Information in a Microsoft SQL Server Database
- 12/19/2007
- Querying a Database by Using ADO.NET
- Querying a Database by Using DLINQ
- Chapter 25 Quick Reference
Chapter 25 Quick Reference
To |
Do This |
Connect to a SQL Server database by using ADO.NET |
Create a SqIConnection object, set its ConnectionString property with details specifying the database to use, and call the Open method. |
Create and execute a database query by using ADO.NET |
Create a SqlCommand object. Set its Connection property to a valid SqlConnection object. Set its CommandText property to a valid SQL SELECT statement. Call the ExecuteReader method to run the query and create a SqlDataReader object. |
Fetch data by using an ADO.NET SqlDataReader object |
Ensure that the data is not null by using the IsDBNull method. If the data is not null, use the appropriate GetXXX method (such as GetString or GetInt32) to retrieve the data. |
Define an entity class |
Define an entity class Define a class with public properties for each column. Prefix the class definition with the Table attribute, specifying the name of the table in the underlying database. Prefix each property with the Column attribute, and specify parameters indicating the name, type, and nullability of the corresponding column in the database. |
Create and execute a query by using DLINQ | Create a DataContext variable, and specify a connection string for the database. Create a Table collection variable based on the entity class corresponding to the table you want to query. Define a DLINQ query that identifies the data to be retrieved from the database and returns an enumerable collection of entities. Iterate through the enumerable collection to retrieve the data for each row and process the results. |