Advertisement
Fhernd

R906Programa.cs

Mar 29th, 2018
1,768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4.  
  5. namespace R905EjecutarComandoSql
  6. {
  7.     class R906Programa
  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.                 conexion.Open();
  16.  
  17.                 Console.WriteLine("Ejemplo de ExecuteNonQuery:\n");
  18.                 ExecuteNonQueryEjemplo(conexion);
  19.  
  20.                 Console.WriteLine(Environment.NewLine);
  21.  
  22.                 Console.WriteLine("Ejemplo de ExecuteReader:\n");
  23.                 ExecuteReaderEjemplo(conexion);
  24.  
  25.                 Console.WriteLine(Environment.NewLine);
  26.  
  27.                 Console.WriteLine("Ejemplo de ExecuteScalar:\n");
  28.                 ExecuteScalarEjemplo(conexion);
  29.  
  30.                 Console.WriteLine("\nPresione Enter para continuar...");
  31.                 Console.ReadLine();
  32.             }
  33.         }
  34.  
  35.         public static void ExecuteNonQueryEjemplo(IDbConnection conexion)
  36.         {
  37.             IDbCommand comando = conexion.CreateCommand();
  38.             comando.CommandType = CommandType.Text;
  39.             comando.CommandText = "UPDATE Employees SET Title = 'Sales Director' WHERE EmployeeId = '5'";
  40.  
  41.             int resultado = comando.ExecuteNonQuery();
  42.  
  43.             if (resultado == 1)
  44.             {
  45.                 Console.WriteLine("Título de empleado -ID = 5- actualizado.");
  46.             }
  47.             else
  48.             {
  49.                 Console.WriteLine("No se pudo actualizar el título del empleado.");
  50.             }
  51.         }
  52.  
  53.         public static void ExecuteReaderEjemplo(IDbConnection conexion)
  54.         {
  55.             IDbCommand comando = conexion.CreateCommand();
  56.             comando.CommandType = CommandType.StoredProcedure;
  57.             comando.CommandText = "Ten Most Expensive Products";
  58.  
  59.             using (IDataReader lector = comando.ExecuteReader())
  60.             {
  61.                 Console.WriteLine("Precio de los 10 productos más costosos:");
  62.  
  63.                 while (lector.Read())
  64.                 {
  65.                     Console.WriteLine("{0} = {1}", lector["TenMostExpensiveProducts"], lector["UnitPrice"]);
  66.                 }
  67.             }
  68.         }
  69.  
  70.         public static void ExecuteScalarEjemplo(IDbConnection conexion)
  71.         {
  72.             IDbCommand comando = conexion.CreateCommand();
  73.             comando.CommandType = CommandType.Text;
  74.             comando.CommandText = "SELECT COUNT(*) FROM Employees";
  75.  
  76.             int resultado = (int) comando.ExecuteScalar();
  77.  
  78.             Console.WriteLine("Cantidad de empleados: {0}", resultado);
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement