Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4.  
  5.  
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. string str = "Data Source=(local);Initial Catalog=Northwind;"
  11. + "Integrated Security=SSPI";
  12. ReadOrderData(str);
  13. }
  14.  
  15. private static void ReadOrderData(string connectionString)
  16. {
  17. string queryString =
  18. "SELECT OrderID, CustomerID FROM dbo.Orders;";
  19.  
  20. using (SqlConnection connection =
  21. new SqlConnection(connectionString))
  22. {
  23. SqlCommand command =
  24. new SqlCommand(queryString, connection);
  25. connection.Open();
  26.  
  27. SqlDataReader reader = command.ExecuteReader();
  28.  
  29. // Call Read before accessing data.
  30. while (reader.Read())
  31. {
  32. ReadSingleRow((IDataRecord)reader);
  33. }
  34.  
  35. // Call Close when done reading.
  36. reader.Close();
  37. }
  38. }
  39.  
  40. private static void ReadSingleRow(IDataRecord record)
  41. {
  42. Console.WriteLine(String.Format("{0}, {1}", record[0], record[1]));
  43. }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement