sashomaga

ADO.NET connect to ms sql server

Jul 31st, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. static void Main()
  2.     {
  3.         //SqlConnection dbCon = new SqlConnection("Server=.\\MSSQL; " +
  4.         //    "Database=TelerikAcademy; Integrated Security=true");
  5.  
  6.  
  7.         SqlConnection dbCon = new SqlConnection("Data Source=localhost; Integrated Security=SSPI;" +
  8.                                             "Initial Catalog=TelerikAcademy");
  9.  
  10.         dbCon.Open();
  11.         using (dbCon)
  12.         {
  13.             SqlCommand cmdCount = new SqlCommand(
  14.                 "SELECT COUNT(*) FROM Employees", dbCon);
  15.             int employeesCount = (int)cmdCount.ExecuteScalar();
  16.             Console.WriteLine("Employees count: {0} ", employeesCount);
  17.             Console.WriteLine();
  18.  
  19.             Console.WriteLine("The most senior 10 employees:");
  20.             SqlCommand cmdAllEmployees = new SqlCommand(
  21.               "SELECT TOP 10 * FROM Employees ORDER BY HireDate", dbCon);
  22.             SqlDataReader reader = cmdAllEmployees.ExecuteReader();
  23.             using (reader)
  24.             {
  25.                 while (reader.Read())
  26.                 {
  27.                     string firstName = (string)reader["FirstName"];
  28.                     string lastName = (string)reader["LastName"];
  29.                     decimal salary = (decimal)reader["Salary"];
  30.                     Console.WriteLine("{0} {1} - {2}", firstName, lastName, salary);
  31.                 }
  32.             }
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment