Advertisement
Guest User

Untitled

a guest
Mar 4th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SqlClient;
  7.  
  8. namespace UsersDataAccess
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. SqlConnection conn = new SqlConnection();
  15. conn.ConnectionString = @"Server=localhost;Database=MyDatabase;Trusted_Connection=True;";
  16. // database seesiq e vremeto mejdu otvarqne na edna vruzka i zatvarqneto
  17.  
  18. SqlCommand cmd = new SqlCommand();
  19. cmd.Connection = conn;
  20. cmd.CommandText = @"
  21. SELECT
  22. Id,
  23. Username,
  24. Password,
  25. FirstName,
  26. LastName
  27. FROM
  28. Users
  29. WHERE
  30. Username = AND Password = ";
  31. SqlDataReader reader = null;
  32. try
  33. {
  34. conn.Open();
  35.  
  36. reader = cmd.ExecuteReader();
  37.  
  38. while (reader.Read() == true) // vrushta true (uspeshno e prochel zapisa ot database) ili false (ne e uspeshno, ne e prochel danni)
  39. {
  40. Console.WriteLine(reader["Id"].ToString());
  41. Console.WriteLine(reader["Username"].ToString());
  42. Console.WriteLine(reader["Password"].ToString());
  43. Console.WriteLine(reader["FirstName"].ToString());
  44. Console.WriteLine(reader["LastName"].ToString());
  45. Console.WriteLine("**********");
  46. }
  47.  
  48. }
  49. catch (Exception ex)
  50. {
  51. Console.WriteLine(ex.Message);
  52. }
  53. finally
  54. {
  55. reader.Close();
  56. conn.Close();
  57. }
  58.  
  59. Console.ReadKey(true);
  60.  
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement