Advertisement
Fhernd

R907Programa.cs

Mar 30th, 2018
2,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4.  
  5. namespace R907LectorDatosConsultaSql
  6. {
  7.     class R907Programa
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             using (SqlConnection conexion = new SqlConnection())
  12.             {
  13.                 conexion.ConnectionString = @"Data source =.\SQLEXPRESS; Initial catalog = Northwind;Integrated Security=SSPI";
  14.  
  15.                 using (SqlCommand comando = conexion.CreateCommand())
  16.                 {
  17.                     comando.CommandType = CommandType.Text;
  18.                     comando.CommandText =
  19.                         "SELECT BirthDate, FirstName, LastName FROM Employees ORDER BY BirthDate;SELECT * FROM Employees";
  20.  
  21.                     conexion.Open();
  22.  
  23.                     using (SqlDataReader lectorDatos = comando.ExecuteReader())
  24.                     {
  25.                         Console.WriteLine("Cumpleaños de los empleados por edad:");
  26.  
  27.                         while (lectorDatos.Read())
  28.                         {
  29.                             Console.WriteLine("\t{0,18:D} - {1} {2}",
  30.                                 lectorDatos.GetDateTime(0),
  31.                                 lectorDatos["FirstName"],
  32.                                 lectorDatos[2]);
  33.                         }
  34.  
  35.                         Console.WriteLine("\nMetados de la tabla `Employees`:");
  36.                         for (int campo = 0; campo < lectorDatos.FieldCount; campo++)
  37.                         {
  38.                             Console.WriteLine("\tNombre Columna: {0} - Tipo: {1}",
  39.                                 lectorDatos.GetName(campo),
  40.                                 lectorDatos.GetDataTypeName(campo));
  41.                         }
  42.                     }
  43.                 }
  44.             }
  45.  
  46.             Console.WriteLine("\n\nPresione Enter para finalizar...");
  47.             Console.ReadLine();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement