วันพฤหัสบดีที่ 2 เมษายน พ.ศ. 2558

Module VB.Net เอาไว้ เพิ่ม ลบ แก้ไข

พอดีวันนี้ได้ ทดสอบ (CRUD) การเขียน Vb.net ปกติจะไม่เขียน เพราะไม่ชอบ vb มาแต่ไหนแต่ไรแล้ว
แต่เพราะจะไปสมัครงานอยู่ที่หนึ่ง เลยลองๆมาเขียนดู แต่ก็สนุกดีน่ะ


อันนี้ส่วนของ Table น่ะ
CREATE TABLE [dbo].[users](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [name] [varchar](50) NULL,
 [username] [varchar](50) NULL,
 [password] [varchar](50) NULL,
 CONSTRAINT [PK_users] 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]




ผมอธิบายไม่เก่ง มาดูโค้ดเลยล่ะกัน
Module ExecuteModule
    Dim conn As SqlClient.SqlConnection
    Dim cmd As New SqlClient.SqlCommand
    Dim adapter As New SqlClient.SqlDataAdapter
    Dim ds As DataSet


    Dim strConn As String = "Server=192.168.0.8;UID=sa;PASSWORD=Admin*9;database=tmpdbms;Max Pool Size=400;Connect Timeout=600;"
    'ส่วนนี้จะเป็นส่วนของการเลือกข้อมูล Select จะส่งค่ากลับเป็น DataSet
    ' 
    Function GetDataSet(ByVal strSql As String) As DataSet
        Try
            conn = New SqlClient.SqlConnection(strConn)

            With cmd
                .Connection = conn
                .CommandText = strSql
                .CommandType = CommandType.Text
            End With
            ds = New DataSet()
            adapter.SelectCommand = cmd
            adapter.Fill(ds)

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        Finally

            conn.Close()
            cmd.Dispose()

        End Try

        Return ds
    End Function

    'ส่วนนี้จะเป็นส่วนของการ insert update delete
    Function Execute(ByVal strSql As String) As Integer
        Dim i As Integer = 0
        Try
            conn = New SqlClient.SqlConnection(strConn)
            conn.Open()
            With cmd
                .Connection = conn
                .CommandText = strSql
                .CommandType = CommandType.Text
            End With
            i = cmd.ExecuteNonQuery()

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        Finally

            conn.Close()
            cmd.Dispose()

        End Try

        Return i
    End Function

    'ส่วนนี้จะเป็นส่วนของการ insert update delete เหมือนกัน แต่เป็นการยัด parameter เข้าไป (โปรแกรมไม่ต้องเรียก sql ซ้ำ)
    Function ExecuteParameter(ByVal strSql As String, ByVal hastTable As Hashtable) As Integer

        Dim i As Integer = 0
        Dim hast As DictionaryEntry
        Try
            conn = New SqlClient.SqlConnection(strConn)
            conn.Open()
            cmd = New SqlClient.SqlCommand(strSql, conn)

            With cmd
                For Each hast In hastTable
                    ' .Parameters.Add(New SqlParameter("@sName","TEST"))
                    .Parameters.AddWithValue(hast.Key, hast.Value)
                Next
            End With

            i = cmd.ExecuteNonQuery()


        Catch ex As Exception
            MessageBox.Show(ex.Message)

        Finally

            conn.Close()
            cmd.Dispose()
        End Try

        Return i
    End Function

    ' ส่วนนี้เป็นการเรียกใช้งาน procedure แล้วส่งค่ากลับเป็น DataSet 
    Function ExecuteProcedure(ByVal strSql As String, ByVal hastTable As Hashtable) As DataSet
        Try
            conn = New SqlClient.SqlConnection(strConn)
            cmd = New SqlClient.SqlCommand(strSql, conn)
            cmd.CommandType = CommandType.StoredProcedure
            With cmd
                For Each hast In hastTable
                    ' .Parameters.Add(New SqlParameter("@sName","TEST"))
                    .Parameters.AddWithValue(hast.Key, hast.Value)
                Next
            End With
            ds = New DataSet()
            adapter.SelectCommand = cmd
            adapter.Fill(ds)

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        Finally

            conn.Close()
            cmd.Dispose()

        End Try

        Return ds

    End Function


End Module



Public Class Form1

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

        Dim strSql As String
        Dim result As Integer
        If Button1.Text = "Save" Then
            strSql = "insert into users values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')"

            result = ExecuteModule.Execute(strSql)

            If result > 0 Then
                MsgBox("Insert Seccess")
            End If
        Else
            strSql = "update users set name= '" + TextBox1.Text + "' , username = '" + TextBox2.Text + "',password = '" + TextBox3.Text + "' "
            strSql &= " where id = '" + Label4.Text + "' "
            result = ExecuteModule.Execute(strSql)

            If result > 0 Then
                MsgBox("Update Seccess")
            End If

        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim strSql As String

        strSql = "select * from users order by id desc"
        Dim ds As New DataSet
        ds = ExecuteModule.GetDataSet(strSql)
        Call ClearValue()
        If (ds.Tables(0).Rows.Count > 0) Then
            Me.DataGridView1.DataSource = ds.Tables(0)
        End If

        Button1.Text = "Save"

    End Sub


    Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        TextBox1.Text = DataGridView1.Item(1, e.RowIndex).Value.ToString
        TextBox2.Text = DataGridView1.Item(2, e.RowIndex).Value.ToString
        TextBox3.Text = DataGridView1.Item(3, e.RowIndex).Value.ToString
        Label4.Text = DataGridView1.Item(0, e.RowIndex).Value.ToString
        Button1.Text = "Update"
    End Sub

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

        Dim strSql As String
        strSql = "delete from users where id = @id "

        Dim obj As New Hashtable

        obj.Add("@id", Label4.Text)

        Dim i As Integer

        If MsgBox("ต้องการลบข้อมูล?", MsgBoxStyle.YesNoCancel, "Title") = MsgBoxResult.Yes Then

            i = ExecuteModule.ExecuteParameter(strSql, obj)

            If (i > 0) Then

                MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว")

            End If
        End If



    End Sub


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

        Dim strSql As String
        strSql = "SP_User"

        Dim obj As New Hashtable

        obj.Add("@parameter1", Convert.ToInt32(TextBox4.Text))


        Dim ds As DataSet
        ds = ExecuteModule.ExecuteProcedure(strSql, obj)

        If (ds.Tables.Count > 0) Then
            Me.DataGridView1.DataSource = ds.Tables(0)
        End If


    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        Call ClearValue()
    End Sub


    Sub ClearValue()
        Me.DataGridView1.DataSource = Nothing
        Me.DataGridView1.Rows.Clear()
        Me.DataGridView1.Columns.Clear()
    End Sub
End Class


ปกผมเขียน แต่ C#,Java,PHP แต่มาเขียน VB.NET ก็ตื่นเต้นไปอีกแบบ
จากที่ไม่ชอบก็เริ่มชอบล่ะ
.... อย่างว่าล่ะครับทุกวันนี้ ถึงแม้เราเก่ง (ถ้าไม่เป็นนายตัวเองน่ะ) เราไปสมัครงาน ความต้องการของแต่ล่ะที่ก็เยอะซ่ะเหลือเกิน
ซึ่งการเขียนโปรแกรมเก่งอย่างเดียวคงไม่พอ ต้องได้ภาษา implement เป็น สอน User ได้ สรุป All in One
ฟังดูเหมือน คุณค่าของโปรแกรมเมอร์มันน้อยซ่ะเหลือเกิน แต่ก็ไม่เป็นไร เราทำแล้วเรามีความสุขที่ได้ทำมัน ผมก็ถือว่าคุ้มล่ะ
วันนี้เอาแค่นี้ก่อน เดียววันหลังหาอะไรมาเขียนเล่นๆ อีก

ไม่แน่ บางทีผมอาจสอน SAP ABAP ก็ได้ เพราะผมศึกษามาได้สักพักใหญ่ๆแล้ว ติดตามตอนต่อไปน่ะครับ..
โปรแกรมมั่ว..

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

การติดตั้ง SAP ABAP

ครั้งแรกผมว่าจะสอนวิธีติดตั้งเอง แต่ก็กลัวเรื่อง ลิขสิทธิ์ (เอ๊ะ หรือว่าคิดมากไปเอง)  เอาเป็นว่า ไปดูเว็บฝรั่งก็ได้น่ะ หรือไม่ก็ youtube ...