Monday, November 7, 2016

Login System in C# and Microsoft SQL Server

Here is a simple login system that I wrote using C# and Microsoft SQL Server 2008 that can used to protect from one's application from intruder and unauthorized users. The program will ask the username and password and it will check the username and password in our Microsoft SQL Server database if it is valid or not valid. If the credentials are valid it will display a welcome page it not valid it will ask again to give the right credentials in order to gain access to the system. I hope you will find my work useful. Thank you.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.













Sample Program Output and Database Structures


Program Listing


using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace login
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to exit?", "Login System",
         MessageBoxButtons.YesNo, MessageBoxIcon.Question)
         == DialogResult.Yes)
            {
                Application.Exit();
            }
            else
            {
                txt_username.Focus();
            }
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
             if(txt_username.Text=="" || txt_password.Text=="")
            {
                MessageBox.Show("Please provide UserName and Password");
                return;
            }
            try
            {
                //Create SqlConnection
                SqlConnection con =  new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=access;Integrated Security=true;");
                SqlCommand cmd = new SqlCommand("Select * from tbl_access where username=@username AND password_1=@password",con);
                cmd.Parameters.AddWithValue("@username",txt_username.Text);
                cmd.Parameters.AddWithValue("@password", txt_password.Text);
                con.Open();
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapt.Fill(ds);
                con.Close();
                int count = ds.Tables[0].Rows.Count;
                string str;

                 str = "select * from tbl_access";
                SqlCommand com = new SqlCommand(str, con);
                con.Open();
                SqlDataReader reader = com.ExecuteReader();


                if (count == 1)
                {
                    MessageBox.Show("Login Successful!");
                    this.Hide();
                    Form2 fm = new Form2();
                    fm.Show();
                  
                 }
                else
                {
                    MessageBox.Show("Login Failed!!! Try Again.");
                    txt_username.Text = "";
                    txt_password.Text = "";
                    txt_username.Focus();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        }
    }


SQL DUMB File

USE [master]
GO
/****** Object:  Database [access]    Script Date: 11/07/2016 08:50:06 ******/
CREATE DATABASE [access] ON  PRIMARY 
( NAME = N'access', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\access.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'access_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\access_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [access] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [access].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [access] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [access] SET ANSI_NULLS OFF
GO
ALTER DATABASE [access] SET ANSI_PADDING OFF
GO
ALTER DATABASE [access] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [access] SET ARITHABORT OFF
GO
ALTER DATABASE [access] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [access] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [access] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [access] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [access] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [access] SET CURSOR_DEFAULT  GLOBAL
GO
ALTER DATABASE [access] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [access] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [access] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [access] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [access] SET  DISABLE_BROKER
GO
ALTER DATABASE [access] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [access] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [access] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [access] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [access] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [access] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [access] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [access] SET  READ_WRITE
GO
ALTER DATABASE [access] SET RECOVERY SIMPLE
GO
ALTER DATABASE [access] SET  MULTI_USER
GO
ALTER DATABASE [access] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [access] SET DB_CHAINING OFF
GO
USE [access]
GO
/****** Object:  Table [dbo].[tbl_access]    Script Date: 11/07/2016 08:50:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_access](
[id] [int] IDENTITY(1,1) NOT NULL,
[username] [nchar](100) NULL,
[password_1] [nchar](100) NULL,
[lastname] [nchar](100) NULL,
[firstname] [nchar](100) NULL,
 CONSTRAINT [PK_tbl_access] PRIMARY KEY CLUSTERED 
(
[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tbl_access] ON
INSERT [dbo].[tbl_access] ([id], [username], [password_1], [lastname], [firstname]) VALUES (1, N'jake                                                                                                ', N'123                                                                                                 ', N'POMPERADA                                                                                           ', N'JAKE                                                                                                ')
INSERT [dbo].[tbl_access] ([id], [username], [password_1], [lastname], [firstname]) VALUES (2, N'allie                                                                                               ', N'iya                                                                                                 ', N'POMPERADA                                                                                           ', N'MA. JUNALLIE                                                                                        ')
INSERT [dbo].[tbl_access] ([id], [username], [password_1], [lastname], [firstname]) VALUES (3, N'iya                                                                                                 ', N'iya                                                                                                 ', N'POMPERADA                                                                                           ', N'JULIANNA RAE                                                                                        ')
SET IDENTITY_INSERT [dbo].[tbl_access] OFF



No comments:

Post a Comment