Advertisement
Guest User

Untitled

a guest
Oct 5th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using DAL.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Forms;
  10.  
  11. namespace DAL
  12. {
  13.     public class LecLogin
  14.     {
  15.         public async Task<LoginInfo> lLogin(String username, String password)
  16.         {
  17.             String queryString =
  18.                 "SELECT [Username] ,[Password] FROM [POETask2].[dbo].[tblLecturerSec] WHERE (Username = @user) and (Password = @pass)";
  19.             String connectionString = DBConnector.getDBCon();
  20.             String user = "";
  21.             using (SqlConnection connection = new SqlConnection(connectionString))
  22.             {
  23.                 SqlCommand cmd = new SqlCommand(queryString, connection);
  24.                 cmd.Parameters.AddWithValue("@user", username);
  25.                 cmd.Parameters.AddWithValue("@pass", password);
  26.                 connection.Open();
  27.                 SqlDataReader sqlReader = cmd.ExecuteReader();
  28.                 try
  29.                 {
  30.                     while (sqlReader.Read())
  31.                     {
  32.                         user = (String.Format("{0}, {1}",
  33.                             sqlReader["username"], sqlReader["password"])); // etc
  34.                     }
  35.                 }
  36.                 catch (Exception ex)
  37.                 {
  38.                     throw ex;
  39.                 }
  40.                 finally
  41.                 {
  42.                     sqlReader.Close(); //Closing the reader
  43.                 }
  44.             }
  45.  
  46.             if (user == "" || password == "")
  47.             {
  48.                 MessageBox.Show("A username or password is required to login or you have entered the incorrect details!");
  49.                 return new LoginInfo { isSuccess = false };
  50.             }
  51.             else
  52.             {
  53.                 MessageBox.Show(username + " has successfully signed in!", "Success!");
  54.                 return new LoginInfo { isSuccess = true, Username = user };
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement