Programming for Everybody: July 2021

How to create login form with multi users and permissions in VB.net using Sql database[with Source code]




 Imports System.Data.SqlClient

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim conn As New SqlConnection("Data source=.;initial catalog=login3;integrated security=true")

        If conn.State = ConnectionState.Closed Then

            conn.Open()

        End If

        Dim cmd As New SqlCommand("Select * from Admin where username='admin' and password=@password", conn)

        cmd.Parameters.AddWithValue("password", TextBox1.Text)

        Dim myreader As SqlDataReader = cmd.ExecuteReader

        If (myreader.Read()) Then

            username_v = myreader("username")

            Form2.Show()

            Me.Hide()

        Else

            MsgBox("Error Password")

        End If

    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Me.Close()

    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Dim conn As New SqlConnection("Data source=.;initial catalog=login3;integrated security=true")

        If conn.State = ConnectionState.Closed Then

            conn.Open()

        End If

        Dim cmd As New SqlCommand("Select * from users where username=@username and password=@password", conn)

        cmd.Parameters.AddWithValue("username", TextBox3.Text)

        cmd.Parameters.AddWithValue("password", TextBox2.Text)

        Dim myreader As SqlDataReader = cmd.ExecuteReader

        If (myreader.Read()) Then

            username_v = myreader("username")

            If Not myreader.IsDBNull(myreader.GetOrdinal("insert")) Then

                CheckBox1.Checked = myreader("insert")

            End If

            If Not myreader.IsDBNull(myreader.GetOrdinal("update")) Then

                CheckBox2.Checked = myreader("update")

            End If

            If Not myreader.IsDBNull(myreader.GetOrdinal("delete")) Then

                CheckBox3.Checked = myreader("delete")

            End If

            If CheckBox1.Checked = False Then

                Form2.Button1.Visible = False

            End If

            If CheckBox2.Checked = False Then

                Form2.Button2.Visible = False

            End If

            If CheckBox3.Checked = False Then

                Form2.Button3.Visible = False

            End If


            Form2.Show()

                Me.Hide()

            Else

                MsgBox("Error Password")

        End If

    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Me.Close()

    End Sub

End Class

ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ

Public Class Form2

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Label1.Text = "Welcome, " & username_v

    End Sub

End Class

ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ

Module Module1

    Public username_v As String

End Module

Backup delete and restore sql server database in VB. net

 

Imports System.Data.SqlClient



Public Class Form3

    Dim conn As New SqlConnection("Data source=.;Integrated security=true")

    Private Sub databases()

        conn.Open()

        ComboBox1.Items.Clear()

        Dim cmd1 As New SqlCommand("Select * from sysdatabases order by name", conn)

        Dim myreader As SqlDataReader = cmd1.ExecuteReader

        While myreader.Read

            ComboBox1.Items.Add(myreader(0))

            ComboBox1.Text = "Select database"

        End While

        conn.Close()

    End Sub

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        databases()

    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If ComboBox1.Text = "Select database" Then

            MessageBox.Show("Please Select database")

            Return

        Else

            SaveFileDialog1.FileName = ComboBox1.SelectedItem

            If SaveFileDialog1.ShowDialog = DialogResult.OK Then

                Dim lfolder As String

                lfolder = SaveFileDialog1.FileName

                Dim cmd2 As New SqlCommand("BACKUP Database " & ComboBox1.Text & " To disk='" & lfolder & "'", conn)

                conn.Open()

                cmd2.ExecuteNonQuery()

                conn.Close()

                MessageBox.Show("database backed up successfully")

            Else

                MessageBox.Show("Please Save database")

            End If

        End If

    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If ComboBox1.Text = "Select database" Then

            MessageBox.Show("Please Select database")

            Return

        Else

            Dim cmd3 As New SqlCommand("Drop database " & ComboBox1.Text & "", conn)

            conn.Open()

            cmd3.ExecuteNonQuery()

            conn.Close()

            MessageBox.Show("database deleted successfully")

            databases()

        End If


    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim dbname As String

        dbname = InputBox("Input database name you want to restore", "Database Name")

        Dim cmd4 As New SqlCommand("Select * from sysdatabases where name = '" & dbname & "'", conn)

        conn.Open()

        Dim myreader2 As SqlDataReader = cmd4.ExecuteReader

        If myreader2.Read Then

            MessageBox.Show("Database exists in sql server")

            conn.Close()

        Else

            conn.Close()

            If OpenFileDialog1.ShowDialog = DialogResult.OK Then

                Dim cmd3 As New SqlCommand("Restore database " & dbname & " from disk='" & OpenFileDialog1.FileName & "'", conn)

                conn.Open()

                cmd3.ExecuteReader()

                conn.Close()

                databases()

                MessageBox.Show("database restored successfully")

            Else

                MessageBox.Show("Please Select database you want to restore")

            End If

        End If

    End Sub

End Class