24-08-2013, 04:30 PM
SqlDataReader
SqlDataReader.docx (Size: 112.49 KB / Downloads: 31)
SqlDataReader reads database rows one-by-one. It reads in forward order from a SQL database. The SqlDataReader type can be used in a loop to read multiple rows from a SQL database. It provides good performance and strong typing.
Several preliminary steps are required before using the SqlDataReader on your database table. Unfortunately, these steps will require some configuration on your part and you must target the correct database with a custom connection string and also target the proper table.
The DataReader Object
The DataReader Object is a stream-based , forward-only, read-only retrieval of query results from the Data Source, which do not update the data in the Data Source. When the ExecuteReader method in SqlCommand Object execute , it will instantiate a SqlClient.SqlDataReader Object in C#.
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
In some situations we need to execute multiple SQL statements with the Command Object. In these types of situations the SqlDataReader returns multiple ResultSets also. In order to retrieving multiple ResultSets from SqlDataReader we use the NextResult() method of the SqlDataReader.
Sql connection
The Connection Object is a part of ADO.NET Data Provider and it is a unique session with the Data Source. The Connection Object is Handling the part of physical communication between the C# application and the Data Source.
The Connection Object connect to the specified Data Source and open a connection between the C# application and the Data Source, depends on the parameter specified in the Connection String . When the connection is established , SQL Commands will execute with the help of the Connection Object and retrieve or manipulate data in the Data Source.
Once the Database activity is over , Connection should be closed and release the Data Source resources .
Add namespaces
If your are working in a console program, you will only need three namespaces. You need to add the System.Data.SqlClient namespace manually. This will give you easy access to the SqlConnection, SqlCommand, and other Sql* classes.