Beruflich Dokumente
Kultur Dokumente
SqlDataReader Signature
1. public class SqlDataReader : System.Data.Common.DbDataReader, IDi
sposable
SqlDataReader Properties
Property Description
Depth It is used to get a value that indicates the depth of nesting for
Item[String] It is used to get the value of the specified column in its native
column name.
Item[Int32] It is used to get the value of the specified column in its native
column ordinal.
RecordsAffected It is used to get the number of rows changed, inserted or dele
of the Transact-SQL statement.
Methods
Method Description
GetValue(Int32) It is used to get the value of the specified column in its nati
Example
In the following program, we are using SqlDataReader to get data from the
SQL Server. A C# code is given below.
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().GetData();
10. }
11. public void GetData()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database=stude
nt; integrated security=SSPI");
18. // writing sql query
19. SqlCommand cm = new SqlCommand("select * from stude
nt", con);
20. // Opening Connection
21. con.Open();
22. // Executing the SQL query
23. SqlDataReader sdr = cm.ExecuteReader();
24. while (sdr.Read())
25. {
26. Console.WriteLine(sdr["name"]+" "+ sdr["email"]);
27. }
28. }
29. catch (Exception e)
30. {
31. Console.WriteLine("OOPs, something went wrong." + e);
32. }
33. // Closing the connection
34. finally
35. {
36. con.Close();
37. }
38. }
39. }
40. }
Output: