Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.Sql;
  7. using System.Data.SqlClient;
  8.  
  9. namespace Server
  10. {
  11.     public sealed class Sql
  12.     {
  13.         SqlConnection sqlConnection;
  14.  
  15.         public Sql()
  16.         {
  17.         }
  18.  
  19.         public void Connect()
  20.         {
  21.             try
  22.             {
  23.                 sqlConnection = new SqlConnection(@"server = .\sqlexpress;
  24.                                                    integrated security = true;
  25.                                                    database = xBot");
  26.                 sqlConnection.Open();
  27.             }
  28.             catch
  29.             {
  30.                 System.Windows.Forms.MessageBox.Show("Cannot connect to database.", "Error",
  31.                     System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
  32.                 Environment.Exit(0);
  33.             }
  34.         }
  35.  
  36.         public int GetID(string userName, string userPassword)
  37.         {
  38.             try
  39.             {
  40.                 if (sqlConnection.State != ConnectionState.Broken && sqlConnection.State != ConnectionState.Closed)
  41.                 {
  42.                     if (userName != null && userPassword != null)
  43.                     {
  44.                         SqlCommand getID = sqlConnection.CreateCommand();
  45.                         getID.CommandText = "select UserID from Users where UserName = @userName and UserPassword = @userPassword";
  46.                         getID.Parameters.Add("@userName", SqlDbType.NChar).Value = userName;
  47.                         getID.Parameters.Add("@userPassword", SqlDbType.NChar).Value = userPassword;
  48.  
  49.                         SqlDataReader reader = getID.ExecuteReader();
  50.                         int id = 0;
  51.                         while (reader.Read())
  52.                         {
  53.                             id = int.Parse(reader["UserID"].ToString());
  54.                         }
  55.                         return id;
  56.                     }
  57.                 }
  58.                 return 0;
  59.             }
  60.             catch
  61.             {
  62.                 return 0;
  63.             }
  64.         }
  65.  
  66.         public bool ChangeUserPassword(string userName, string oldPassword, string newPassword)
  67.         {
  68.             try
  69.             {
  70.                 if (sqlConnection.State != ConnectionState.Broken && sqlConnection.State != ConnectionState.Closed)
  71.                 {
  72.                     if (userName != null && oldPassword != null && newPassword != null)
  73.                     {
  74.                         SqlCommand changePassword = sqlConnection.CreateCommand();
  75.                         changePassword.CommandText = @"update Users
  76.                                                       set UserPassword = @newPassword
  77.                                                       where UserName = @userName and UserPassword = @oldPassword";
  78.                         changePassword.Parameters.Add("@userName", SqlDbType.NChar).Value = userName;
  79.                         changePassword.Parameters.Add("@newPassword", SqlDbType.NChar).Value = newPassword;
  80.                         changePassword.Parameters.Add("@oldPassword", SqlDbType.NChar).Value = oldPassword;
  81.  
  82.                         if (changePassword.ExecuteNonQuery() != 0) return true;
  83.                         else return false;
  84.  
  85.                     }
  86.                 }
  87.                 return false;
  88.             }
  89.             catch
  90.             {
  91.                 return false;
  92.             }
  93.         }
  94.  
  95.         public bool CreateAccount(string userName, string password, string eMail)
  96.         {
  97.             try
  98.             {
  99.                 if (sqlConnection.State != ConnectionState.Broken && sqlConnection.State != ConnectionState.Closed)
  100.                 {
  101.                     if (userName != null && password != null && eMail != null)
  102.                     {
  103.                         SqlCommand checkForUser = sqlConnection.CreateCommand();
  104.                         checkForUser.CommandText = "select * from Users where UserName = @userName";
  105.                         checkForUser.Parameters.Add("@userName", SqlDbType.NChar).Value = userName;
  106.                         SqlDataReader reader = checkForUser.ExecuteReader();
  107.  
  108.                         if (!reader.HasRows)
  109.                         {
  110.                             reader.Close();
  111.                             SqlCommand create = sqlConnection.CreateCommand();
  112.                             create.CommandText = @"insert into Users (UserName,UserPassword,UserEmail,RegistrationDate)
  113.                                                   values (@userName,@userPassword,@userEmail,@registrationDate)";
  114.                             create.Parameters.Add("@userName", SqlDbType.NChar).Value = userName;
  115.                             create.Parameters.Add("@userPassword", SqlDbType.NChar).Value = password;
  116.                             create.Parameters.Add("@userEmail", SqlDbType.NChar).Value = eMail;
  117.                             create.Parameters.Add("@registrationDate", SqlDbType.DateTime).Value = DateTime.Now.ToShortDateString();
  118.  
  119.                             if (create.ExecuteNonQuery() != 0)
  120.                                 return true;
  121.                             else return false;
  122.                         }
  123.                         else
  124.                         {
  125.                             reader.Close();
  126.                             return false;
  127.                         }
  128.                     }
  129.  
  130.                 }
  131.                 return false;
  132.             }
  133.             catch (Exception ex)
  134.             {
  135.                 System.Windows.Forms.MessageBox.Show(ex.Message);
  136.                 return false;
  137.             }
  138.         }
  139.  
  140.         public bool DeleteUser(string userName, string userPassword)
  141.         {
  142.             try
  143.             {
  144.                 if (sqlConnection.State != ConnectionState.Broken && sqlConnection.State != ConnectionState.Closed)
  145.                 {
  146.                     if (userName != null && userPassword != null)
  147.                     {
  148.                         SqlCommand deleteUser = sqlConnection.CreateCommand();
  149.                         deleteUser.CommandText = "delete from Users where UserName = @userName and UserPassword = @userPassword";
  150.                         deleteUser.Parameters.Add("@userName", SqlDbType.NChar).Value = userName;
  151.                         deleteUser.Parameters.Add("@userPassword", SqlDbType.NChar).Value = userPassword;
  152.                         if (deleteUser.ExecuteNonQuery() != 0)
  153.                             return true;
  154.                         else
  155.                             return false;
  156.                     }
  157.                 }
  158.                 return false;
  159.             }
  160.             catch
  161.             {
  162.                 return false;
  163.             }
  164.         }
  165.  
  166.         public DataTable RetrieveUsers()
  167.         {
  168.             try
  169.             {
  170.                 SqlCommand getUsers = sqlConnection.CreateCommand();
  171.                 getUsers.CommandText = "select * from Users";
  172.                 SqlDataReader reader = getUsers.ExecuteReader();
  173.                 DataTable Users = new DataTable("Users");
  174.                 for (int i = 0; i < reader.FieldCount; i++)
  175.                 {
  176.                     Users.Columns.Add(new DataColumn(reader.GetName(i)));
  177.                 }
  178.                 while (reader.Read())
  179.                 {
  180.                     object[] values = new object[reader.FieldCount];
  181.                     for (int i = 0; i < values.Length; i++)
  182.                     {
  183.                         values[i] = reader.GetValue(i);
  184.                     }
  185.                     Users.Rows.Add(values);
  186.                 }
  187.                 reader.Close();
  188.                 return Users;
  189.             }
  190.             catch
  191.             {
  192.                 return null;
  193.             }
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement