Option Strict Off
Imports System.Data.SqlClient
Public Class 系統使用者管理
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet
    Dim dr As SqlDataReader
    Private Sub Set_使用者清單()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 姓名, 帳號, 密碼 FROM 使用者"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
        Set_grid()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 70 : DataGridView1.Columns(1).Width = 70 : DataGridView1.Columns(2).Width = 70
    End Sub
    Private Sub 系統使用者管理_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = 楠晉鞋業AAS : Me.AutoScroll = True
        Button1.Enabled = False
        Set_使用者清單()
    End Sub
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells("姓名").Value.ToString : TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells("帳號").Value
        TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells("密碼").Value
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = "" : Button4.Enabled = False : Button1.Enabled = True
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "INSERT INTO 使用者 (姓名, 帳號, 密碼) " &
               "VALUES (N'" & TextBox1.Text & "',N'" & TextBox2.Text & "',N'" & TextBox3.Text & "')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        MsgBox("新增完成")
        Set_使用者清單() : Set_grid() : Button1.Enabled = False : Button4.Enabled = True
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
            MsgBox("輸入資料有誤")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 使用者 SET 姓名 = N'" & TextBox1.Text & "', 密碼 = N'" & TextBox3.Text & "' WHERE (帳號 = '" & TextBox2.Text & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close() : conn.Close()
            MsgBox("新增完成")
        End If
        Set_使用者清單() : Set_grid()
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        SQL1 = "DELETE FROM 使用者 WHERE (帳號 = '" & TextBox2.Text & "')"
        cmd.CommandText = SQL1 : cmd.Connection = conn
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        Dim aa As MsgBoxResult
        aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then : cmd.ExecuteNonQuery() : cmd.ExecuteNonQuery() : MsgBox("刪除完成") : End If
        conn.Close() : conn.Close()
        TextBox1.Text = "" : TextBox2.Text = "" : TextBox2.Text = ""
        Set_使用者清單() : Set_grid()
    End Sub
End Class
