AbidMufasa

UserDB.cs

Jul 30th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Web;
  8.  
  9. namespace CRUDOperationAJAX.Models
  10. {
  11.     public class UserDB
  12.     {
  13.         //declare connection string  
  14.         string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  15.  
  16.         //Return list of all Users  
  17.         public List<User> ListAll()
  18.         {
  19.             List<User> lst = new List<User>();
  20.             using (SqlConnection con = new SqlConnection(cs))
  21.             {
  22.                 con.Open();
  23.                 SqlCommand com = new SqlCommand("SelectUser", con);
  24.                 com.CommandType = CommandType.StoredProcedure;
  25.                 SqlDataReader rdr = com.ExecuteReader();
  26.                 while (rdr.Read())
  27.                 {
  28.                     lst.Add(new User
  29.                     {
  30.                         UserId = Convert.ToInt32(rdr["UserId"]),
  31.                         UserName = rdr["UserName"].ToString(),
  32.                         IsActive = Convert.ToBoolean(rdr["IsActive"]),
  33.                         Password = rdr["Password"].ToString(),
  34.                        
  35.                     });
  36.                 }
  37.                 return lst;
  38.             }
  39.         }
  40.  
  41.         //Method for Adding an User  
  42.         public bool Add(User usr)
  43.         {
  44.            
  45.             int i;
  46.             using (SqlConnection con = new SqlConnection(cs))
  47.             {
  48.                 con.Open();
  49.                 SqlCommand com = new SqlCommand("InsertUpdateUser", con);
  50.                 com.CommandType = CommandType.StoredProcedure;
  51.                 com.Parameters.AddWithValue("@Id", usr.UserId);
  52.                 com.Parameters.AddWithValue("@UserName", usr.UserName);
  53.                 com.Parameters.AddWithValue("@Password", usr.Password);
  54.                 com.Parameters.AddWithValue("@IsActive", usr.IsActive);
  55.                 com.Parameters.AddWithValue("@Action", "Insert");
  56.                 i = com.ExecuteNonQuery();
  57.             }
  58.             if(i==1)
  59.             return true;
  60.             return false;
  61.         }
  62.  
  63.         //Method for Updating User record  
  64.         public bool Update(User usr)
  65.         {
  66.             int i;
  67.             using (SqlConnection con = new SqlConnection(cs))
  68.             {
  69.                 con.Open();
  70.                 SqlCommand com = new SqlCommand("InsertUpdateUser", con);
  71.                 com.CommandType = CommandType.StoredProcedure;
  72.                 com.Parameters.AddWithValue("@Id", usr.UserId);
  73.                 com.Parameters.AddWithValue("@UserName", usr.UserName);
  74.                 com.Parameters.AddWithValue("@Password", usr.Password);
  75.                 com.Parameters.AddWithValue("@IsActive", usr.IsActive);
  76.                 com.Parameters.AddWithValue("@Action", "Update");
  77.                 i = com.ExecuteNonQuery();
  78.             }
  79.             if (i == 1)
  80.                 return true;
  81.             return false;
  82.         }
  83.  
  84.         //Method for Deleting an User  
  85.         public bool Delete(int ID)
  86.         {
  87.             int i;
  88.             using (SqlConnection con = new SqlConnection(cs))
  89.             {
  90.                 con.Open();
  91.                 SqlCommand com = new SqlCommand("DeleteUser", con);
  92.                 com.CommandType = CommandType.StoredProcedure;
  93.                 com.Parameters.AddWithValue("@Id", ID);
  94.                 i = com.ExecuteNonQuery();
  95.             }
  96.             if (i == 1)
  97.                 return true;
  98.             return false;
  99.         }
  100.  
  101.         public bool AddUser(User usr)
  102.         {
  103.  
  104.             int i;
  105.             using (SqlConnection con = new SqlConnection(cs))
  106.             {
  107.                 con.Open();
  108.                 SqlCommand com = new SqlCommand("InsertUpdateUser", con);
  109.                 com.CommandType = CommandType.StoredProcedure;
  110.                 com.Parameters.AddWithValue("@Id", usr.UserId);
  111.                 com.Parameters.AddWithValue("@UserName", usr.UserName);
  112.                 com.Parameters.AddWithValue("@Password", usr.Password);
  113.                 com.Parameters.AddWithValue("@IsActive", usr.IsActive);
  114.                 com.Parameters.AddWithValue("@Action", "Insert");
  115.                 i = com.ExecuteNonQuery();
  116.             }
  117.             if (i == 1)
  118.                 return true;
  119.             return false;
  120.         }
  121.  
  122.     }
  123. }
Add Comment
Please, Sign In to add comment