Advertisement
Androide28

Execute SP.

Apr 7th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. public struct NomAp
  2. {
  3.     public string Nombre;
  4.     public string Apellido;
  5. }
  6.  
  7. public Dictionary<NomAp, string> dicSelect(string connectionString)
  8. {
  9.     Dictionary<NomAp, string> dicResult = new Dictionary<NomAp, string>();
  10.     using (SqlConnection conn = new SqlConnection(connectionString))
  11.     {
  12.         using (SqlCommand cmd = new SqlCommand("spSelect", conn) { CommandType = CommandType.StoredProcedure })
  13.         {
  14.             conn.Open();
  15.             using(SqlDataReader reader = cmd.ExecuteReader())
  16.             {
  17.                 while(reader.Read())
  18.                 {
  19.                     string strNombre = reader.GetString(0);
  20.                     string strApellido = reader.GetString(1);
  21.                     string strMail = reader.GetString(2);
  22.  
  23.                     NomAp pair = new NomAp { Nombre = strNombre, Apellido = strApellido };
  24.                     dicResult.Add(pair, strMail);
  25.                 }
  26.             }
  27.         }
  28.     }
  29.     return dicResult;
  30. }
  31.  
  32.  
  33. static void Main(string[] args)
  34. {
  35.     clsSQL sql = new clsSQL();
  36.     string strEstado = "";
  37.  
  38.     try
  39.     {
  40.         strEstado = sql.isConnected(sql.strConn()) ? "CONECTADO" : "NO CONECTADO";
  41.  
  42.         Console.WriteLine("Estado de la conexión: {0}\n", strEstado);
  43.         foreach (KeyValuePair<clsSQL.NomAp, string> kp in sql.dicSelect(sql.strConn()))
  44.         {
  45.             Console.WriteLine("Nombre: {0} {1} Mail: {2}", kp.Key.Nombre, kp.Key.Apellido, kp.Value);
  46.         }
  47.     }
  48.     catch(SqlException ex)
  49.     {
  50.         Console.WriteLine("SQL Server Error: {0}", ex.Message);
  51.     }
  52.     Console.ReadLine();
  53. }
  54.  
  55.  
  56. CREATE PROC spSelect
  57. AS
  58. BEGIN
  59.     SELECT TOP 5 p1.FirstName, p1.LastName, p2.EMailAddress
  60.     FROM Person.Person p1, Person.EmailAddress p2
  61.     WHERE p2.BusinessEntityID = p1.BusinessEntityID
  62. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement