Public Class frmMain
'By https://www.thaicreate.com (mr.win)'
Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
Me.Hide()
Dim f As New frmHome
f.Show()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
If MessageBox.Show("Are you sure to exit?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Application.Exit()
End If
End Sub
End Class
frmHome
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Data.SqlTypes
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class frmHome
'By https://www.thaicreate.com (mr.win)'
Private Sub frmHome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindDataGrid()
End Sub
Private Sub BindDataGrid()
Dim myConnection As SqlCeConnection
Dim dt As New DataTable
Dim Adapter As SqlCeDataAdapter
'myConnection = New SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\Database1.sdf;"))
myConnection = New SqlCeConnection("Data Source=C:\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;")
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT [id], [name], [email] FROM [mytable]"
myCommand.CommandType = CommandType.Text
Adapter = New SqlCeDataAdapter(myCommand)
Adapter.Fill(dt)
myConnection.Close()
Me.dgName.DataSource = dt
Me.dgName.Columns.Clear()
Dim column As DataGridViewTextBoxColumn
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "id"
column.HeaderText = "ID"
column.Width = 50
Me.dgName.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "name"
column.HeaderText = "Name"
column.Width = 100
Me.dgName.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "email"
column.HeaderText = "Email"
column.Width = 150
Me.dgName.Columns.Add(column)
dt = Nothing
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Me.Hide()
Dim f As New frmAdd
f.Show()
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Me.Hide()
Dim f As New frmEdit()
f._strID = Me.dgName(0, Me.dgName.CurrentCell.RowIndex).Value.ToString()
f.Show()
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
If MessageBox.Show("Are you sure to delete?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Dim strID As String = Me.dgName(0, Me.dgName.CurrentCell.RowIndex).Value.ToString()
Dim myConnection As SqlCeConnection
'myConnection = New SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\Database1.sdf;"))
myConnection = New SqlCeConnection("Data Source=C:\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;")
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "DELETE FROM [mytable] WHERE id = '" & strID & "'"
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Delete Successfully")
BindDataGrid()
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Hide()
Dim f As New frmMain
f.Show()
End Sub
End Class
frmAdd
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Data.SqlTypes
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class frmAdd
'By https://www.thaicreate.com (mr.win)'
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Me.txtName.Text = "" Then
MessageBox.Show("Please input (Name)")
Me.txtName.Focus()
Exit Sub
End If
If Me.txtEmail.Text = "" Then
MessageBox.Show("Please input (Email)")
Me.txtEmail.Focus()
Exit Sub
End If
Dim myConnection As SqlCeConnection
'myConnection = New SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\Database1.sdf;"))
myConnection = New SqlCeConnection("Data Source=C:\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;")
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "INSERT INTO [mytable] ([name], [email]) VALUES " & _
" ('" & Me.txtName.Text & "','" & Me.txtEmail.Text & "' ) "
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Save Successfully.")
Me.Hide()
Dim f As New frmHome
f.Show()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Hide()
Dim f As New frmHome
f.Show()
End Sub
End Class
frmEdit
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Data.SqlTypes
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class frmEdit
'By https://www.thaicreate.com (mr.win)'
Dim strID As String = ""
Public Property _strID() As String
Get
Return strID
End Get
Set(ByVal value As String)
strID = value
End Set
End Property
Private Sub frmEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myConnection As SqlCeConnection
Dim dt As New DataTable
Dim Adapter As SqlCeDataAdapter
'myConnection = New SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\Database1.sdf;"))
myConnection = New SqlCeConnection("Data Source=C:\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;")
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT [id], [name], [email] FROM [mytable] WHERE id = '" & strID & "' "
myCommand.CommandType = CommandType.Text
Adapter = New SqlCeDataAdapter(myCommand)
Adapter.Fill(dt)
myConnection.Close()
If dt.Rows.Count > 0 Then
Me.txtName.Text = dt.Rows(0)("name")
Me.txtEmail.Text = dt.Rows(0)("email")
End If
dt = Nothing
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Me.txtName.Text = "" Then
MessageBox.Show("Please input (Name)")
Me.txtName.Focus()
Exit Sub
End If
If Me.txtEmail.Text = "" Then
MessageBox.Show("Please input (Email)")
Me.txtEmail.Focus()
Exit Sub
End If
Dim myConnection As SqlCeConnection
'myConnection = New SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\Database1.sdf;"))
myConnection = New SqlCeConnection("Data Source=C:\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;")
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "UPDATE [mytable] SET " & _
" [name] = '" & Me.txtName.Text & "', [email] = '" & Me.txtEmail.Text & "' " & _
" WHERE id = '" & strID & "' "
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Update Successfully")
Me.Hide()
Dim f As New frmHome
f.Show()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Hide()
Dim f As New frmHome
f.Show()
End Sub
End Class
ทดสอบการรันโปรแกรม
โดยคลิกที่ Startup Debugging
หน้าจอหลัก
หน้า Home ซึ่งใช้ DataGridView เพื่อดึงข้อมูลออกมาแสดง