Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data.SqlClient;
- using System.IO;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace MyDatabase
- {
- public partial class frmDefault : Form
- {
- public frmDefault()
- {
- InitializeComponent();
- }
- //Global variables
- private string fileName = "MyDatabase";
- private string fileDirectory = AppDomain.CurrentDomain.BaseDirectory;
- //Checking if the database file already exists
- public static bool doesFileExist(string directory, string fileName)
- {
- if (File.Exists(directory + fileName))
- {
- return true;
- }
- return false;
- }
- //Verifying the login credentials
- private bool loginVerification(string username, string password)
- {
- if (!doesFileExist(fileDirectory, fileName + ".mdf"))
- {
- //SQL Variables
- String sql;
- SqlConnection databaseConnection = new SqlConnection("Server=localhost;Integrated security=SSPI;database=master");
- //Create database
- try
- {
- databaseConnection.Open();
- sql = "CREATE DATABASE " + fileName + " ON PRIMARY " +
- "(NAME = " + fileName + "_Data, " +
- "FILENAME = '" + fileDirectory + fileName + "Data.mdf', " +
- "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
- "LOG ON (NAME = " + fileName + "_Log, " +
- "FILENAME = '" + fileDirectory + fileName + "Log.ldf', " +
- "SIZE = 1MB, " +
- "MAXSIZE = 5MB, " +
- "FILEGROWTH = 10%)";
- SqlCommand nonqueryCommand = new SqlCommand(sql, databaseConnection);
- nonqueryCommand.ExecuteNonQuery();
- //Create table for login
- nonqueryCommand.CommandText = "CREATE TABLE Users (username CHAR PRIMARY KEY, password CHAR)";
- //Insert default login credentials
- nonqueryCommand.CommandText = "INSERT INTO Users VALUES ('admin', 'password')";
- }
- catch (SqlException ex)
- {
- Console.WriteLine(ex.ToString());
- }
- finally
- {
- if (databaseConnection.State == ConnectionState.Open)
- {
- databaseConnection.Close();
- }
- }
- //Login
- try
- {
- SqlConnection loginConnection = new SqlConnection("Server=localhost;Integrated security=SSPI;database=master");
- loginConnection.Open();
- if (loginConnection.State == ConnectionState.Open)
- {
- SqlCommand cmd = new SqlCommand("SELECT Count(*) FROM Users WHERE username=" + username + " and password=" + password, loginConnection);
- SqlDataReader dr = cmd.ExecuteReader();
- if (dr.HasRows)
- {
- return true;
- }
- else
- {
- MessageBox.Show("Invalid credentials.");
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- else
- {
- MessageBox.Show("File exists?");
- }
- return false;
- }
- private void btnLoginPressed(object sender, EventArgs e)
- {
- String userUsername = txtUsername.Text;
- String userPassword = txtPassword.Text;
- if (loginVerification(userUsername, userPassword))
- {
- this.Hide();
- frmControlPanel controlPanel = new frmControlPanel();
- controlPanel.Show();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment