Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created March 30, 2018 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/3cc91044b158314b57ca8934cb2824e1 to your computer and use it in GitHub Desktop.
Save Fhernd/3cc91044b158314b57ca8934cb2824e1 to your computer and use it in GitHub Desktop.
Lectura de datos de una consulta SQL. OrtizOL.
using System;
using System.Data;
using System.Data.SqlClient;
namespace R907LectorDatosConsultaSql
{
class R907Programa
{
static void Main(string[] args)
{
using (SqlConnection conexion = new SqlConnection())
{
conexion.ConnectionString = @"Data source =.\SQLEXPRESS; Initial catalog = Northwind;Integrated Security=SSPI";
using (SqlCommand comando = conexion.CreateCommand())
{
comando.CommandType = CommandType.Text;
comando.CommandText =
"SELECT BirthDate, FirstName, LastName FROM Employees ORDER BY BirthDate;SELECT * FROM Employees";
conexion.Open();
using (SqlDataReader lectorDatos = comando.ExecuteReader())
{
Console.WriteLine("Cumpleaños de los empleados por edad:");
while (lectorDatos.Read())
{
Console.WriteLine("\t{0,18:D} - {1} {2}",
lectorDatos.GetDateTime(0),
lectorDatos["FirstName"],
lectorDatos[2]);
}
Console.WriteLine("\nMetados de la tabla `Employees`:");
for (int campo = 0; campo < lectorDatos.FieldCount; campo++)
{
Console.WriteLine("\tNombre Columna: {0} - Tipo: {1}",
lectorDatos.GetName(campo),
lectorDatos.GetDataTypeName(campo));
}
}
}
}
Console.WriteLine("\n\nPresione Enter para finalizar...");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment