Advertisement
Fhernd

R906Programa.cs

Mar 30th, 2018
1,835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4.  
  5. namespace R906ParametrizarComandoSql
  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 con parametrización de comando SQL:");
  18.                 ParametrizarComando(conexion, "5", "Cleaner");
  19.  
  20.                 Console.WriteLine("\nEjemplo con parametrización de procedimiento almacenado:");
  21.                 ParametrizarProcedimientoAlmacenado(conexion, "Seafood", "1999");
  22.             }
  23.  
  24.             Console.WriteLine("\nPresione Enter para continuar...");
  25.             Console.ReadLine();
  26.         }
  27.  
  28.         public static void ParametrizarComando(SqlConnection conexion, string idEmpleado, String titulo)
  29.         {
  30.             using (SqlCommand comando = conexion.CreateCommand())
  31.             {
  32.                 comando.CommandType = CommandType.Text;
  33.                 comando.CommandText = "UPDATE Employees SET Title = @title WHERE EmployeeId = @id";
  34.  
  35.                 SqlParameter paramentro1 = comando.CreateParameter();
  36.                 paramentro1.ParameterName = "@title";
  37.                 paramentro1.SqlDbType = SqlDbType.VarChar;
  38.                 paramentro1.Value = titulo;
  39.                 comando.Parameters.Add(paramentro1);
  40.  
  41.                 comando.Parameters.Add("@id", SqlDbType.Int).Value = idEmpleado;
  42.  
  43.                 int resultado = comando.ExecuteNonQuery();
  44.  
  45.                 if (resultado == 1)
  46.                 {
  47.                     Console.WriteLine("El empleado con ID {0} ahora tiene el título de {1}", idEmpleado, titulo);
  48.                 }
  49.                 else
  50.                 {
  51.                     Console.WriteLine("No se pudo actualizar el titulo del empleado con ID {0}", idEmpleado);
  52.                 }
  53.             }
  54.         }
  55.  
  56.         public static void ParametrizarProcedimientoAlmacenado(SqlConnection conexion, string categoria, string ahnio)
  57.         {
  58.             using (SqlCommand comando = conexion.CreateCommand())
  59.             {
  60.                 comando.CommandType = CommandType.StoredProcedure;
  61.                 comando.CommandText = "SalesByCategory";
  62.  
  63.                 comando.Parameters.Add("@CategoryName", SqlDbType.NVarChar).Value = categoria;
  64.  
  65.                 comando.Parameters.Add("@OrdYear", SqlDbType.NVarChar).Value = ahnio;
  66.  
  67.                 using (IDataReader lector = comando.ExecuteReader())
  68.                 {
  69.                     Console.WriteLine("Ventas por categoría {0} y año {1}:", categoria, ahnio);
  70.  
  71.                     while (lector.Read())
  72.                     {
  73.                         Console.WriteLine(" {0} = {1}", lector["ProductName"], lector["TotalPurchase"]);
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement