Guest User

Untitled

a guest
Jan 18th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. using System;
  2. using System.Data.SqlClient;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace MyDatabase
  14. {
  15. public partial class frmDefault : Form
  16. {
  17. public frmDefault()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. //Global variables
  23. private string fileName = "MyDatabase";
  24. private string fileDirectory = AppDomain.CurrentDomain.BaseDirectory;
  25.  
  26. //Checking if the database file already exists
  27. public static bool doesFileExist(string directory, string fileName)
  28. {
  29. if (File.Exists(directory + fileName))
  30. {
  31. return true;
  32. }
  33. return false;
  34. }
  35.  
  36. //Verifying the login credentials
  37. private bool loginVerification(string username, string password)
  38. {
  39. if (!doesFileExist(fileDirectory, fileName + ".mdf"))
  40. {
  41. //SQL Variables
  42. String sql;
  43. SqlConnection databaseConnection = new SqlConnection("Server=localhost;Integrated security=SSPI;database=master");
  44.  
  45. //Create database
  46. try
  47. {
  48. databaseConnection.Open();
  49. sql = "CREATE DATABASE " + fileName + " ON PRIMARY " +
  50. "(NAME = " + fileName + "_Data, " +
  51. "FILENAME = '" + fileDirectory + fileName + "Data.mdf', " +
  52. "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
  53. "LOG ON (NAME = " + fileName + "_Log, " +
  54. "FILENAME = '" + fileDirectory + fileName + "Log.ldf', " +
  55. "SIZE = 1MB, " +
  56. "MAXSIZE = 5MB, " +
  57. "FILEGROWTH = 10%)";
  58.  
  59. SqlCommand nonqueryCommand = new SqlCommand(sql, databaseConnection);
  60.  
  61. nonqueryCommand.ExecuteNonQuery();
  62.  
  63. //Create table for login
  64. nonqueryCommand.CommandText = "CREATE TABLE Users (username CHAR PRIMARY KEY, password CHAR)";
  65.  
  66. //Insert default login credentials
  67. nonqueryCommand.CommandText = "INSERT INTO Users VALUES ('admin', 'password')";
  68. }
  69. catch (SqlException ex)
  70. {
  71. Console.WriteLine(ex.ToString());
  72. }
  73. finally
  74. {
  75. if (databaseConnection.State == ConnectionState.Open)
  76. {
  77. databaseConnection.Close();
  78. }
  79. }
  80.  
  81. //Login
  82. try
  83. {
  84. SqlConnection loginConnection = new SqlConnection("Server=localhost;Integrated security=SSPI;database=master");
  85. loginConnection.Open();
  86. if (loginConnection.State == ConnectionState.Open)
  87. {
  88. SqlCommand cmd = new SqlCommand("SELECT Count(*) FROM Users WHERE username=" + username + " and password=" + password, loginConnection);
  89. SqlDataReader dr = cmd.ExecuteReader();
  90. if (dr.HasRows)
  91. {
  92. return true;
  93. }
  94. else
  95. {
  96. MessageBox.Show("Invalid credentials.");
  97. }
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. MessageBox.Show(ex.Message);
  103. }
  104. }
  105. else
  106. {
  107. MessageBox.Show("File exists?");
  108. }
  109. return false;
  110. }
  111.  
  112. private void btnLoginPressed(object sender, EventArgs e)
  113. {
  114. String userUsername = txtUsername.Text;
  115. String userPassword = txtPassword.Text;
  116.  
  117. if (loginVerification(userUsername, userPassword))
  118. {
  119. this.Hide();
  120. frmControlPanel controlPanel = new frmControlPanel();
  121. controlPanel.Show();
  122. }
  123. }
  124. }
  125. }
Add Comment
Please, Sign In to add comment