Advertisement
Guest User

Untitled

a guest
May 21st, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8. using System.Data;
  9. using System.Data.Sql;
  10. using System.Data.SqlClient;
  11.  
  12. namespace WcfService1
  13. {    
  14.     public class Service1 : IPractico1
  15.     {
  16.         /// <summary>
  17.         /// Verifica si el usuario esta dentro de la base de datos.
  18.         /// </summary>
  19.         /// <param name="userName">Nombre de usuario a buscar.</param>
  20.         /// <returns>True or False</returns>
  21.         public bool ExisteUsuario(string userName)
  22.         {
  23.             //OJO ING.!
  24.             //Obviamente todo este codigo deberia ir en una capa de accesso a datos.
  25.             //Pero por la simplicidad del ejercicio, creo que no amerita tanta arquitectura. :P
  26.  
  27.             //Configuramos toda la informacion necesaria para la coneccion SQL.
  28.             string Server = "localhost";
  29.             string Username = "sergiolas";
  30.             string Password = "1234";
  31.             string Database = "dapcuatro";
  32.  
  33.             string ConnectionString = "Data Source=" + Server + ";";
  34.             ConnectionString += "User ID=" + Username + ";";
  35.             ConnectionString += "Password=" + Password + ";";
  36.             ConnectionString += "Initial Catalog=" + Database;
  37.  
  38.             var dbConnection = new SqlConnection();
  39.  
  40.             try
  41.             {
  42.                 dbConnection.ConnectionString = ConnectionString;
  43.                 dbConnection.Open();
  44.             }
  45.             catch (Exception ex)
  46.             {
  47.                 if (dbConnection != null)
  48.                 {
  49.                     dbConnection.Dispose();
  50.                 }
  51.  
  52.                 string error = "No se pudo iniciar la coneccion SQL.";
  53.                 error += Environment.NewLine;
  54.                 error += Environment.NewLine;
  55.                 error += ex.Message;
  56.             }
  57.  
  58.             //Aqui lo que decidi hacer es hacer un select * de la table de Usuarios y logicamente,
  59.             //si existe el usuario solicitado el datatable va tener mas de 0 rows.
  60.             string sentenciaSQL = "Select * from Usuarios where userID = " + userName;
  61.             var dbAdapter = new SqlDataAdapter(sentenciaSQL, dbConnection);
  62.             var dtResultado = new DataTable();
  63.             dbAdapter.Fill(dtResultado);
  64.  
  65.             if (dtResultado.Rows.Count > 0)
  66.             {
  67.                 return true;
  68.             }
  69.             else
  70.             {
  71.                 return false;
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement