Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class chfrm3
Dim conn As New MySqlConnection
Dim str As String = "server=localhost;User id=root;Password=1234;Database=student_att"
Private Sub chfrm3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ports As String() = SerialPort.GetPortNames()
'ComPort = "aaa"
' Show a label with Action information on it
'lblComFound.Text = "The following serial ports were found:"
' Put each port name Into a comboBox control.
Dim port As String
Dim strport As String
For Each port In ports
'Cboport.Items.Add(port).ToString()
strport = CboPort.Items.Add(port).ToString()
'strport.ToString()
Next port
' Select the first item in the combo control
'Cboport.SelectedIndex = 0
CboPort.SelectedIndex = 0
End Sub
'Private Sub CboDiscon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Dim discon As String = CboPort.Text
' CboPort.Text = ""
' Cbobuad.Text = ""
' CboBit.Text = ""
' Cboparity.Text = ""
' CboStb.Text = ""
' TextBox1.Text = ""
' TextBox1.Text = discon & " Disconnected"
'End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub Btnconne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnconne.Click
' Dim dt As String = "INSERT INTO comport (Port, BitperSec, Databit, Parity, StopBits) VALUES ('" & CboPort.Text & "','" & Cbobuad.Text & "','" & CboBit.Text & "','" & Cboparity.Text & "','" & CboStb.Text & "')"
Dim dt As String = "update comport set Port='" & CboPort.Text & "',BitperSec='" & Cbobuad.Text & "',Databit='" & CboBit.Text & "',Parity='" & Cboparity.Text & "',StopBits='" & CboStb.Text & "' where IdPort= '27';"
Dim cboshow As String = CboPort.Text
conn.ConnectionString = str
If CboPort.Text = "" Or Cbobuad.Text = "" Or Cboparity.Text = "" Or CboStb.Text = "" Or CboBit.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมุลให้ครบด้วยค่ะ")
Else
Try
If conn.State = ConnectionState.Closed Then
conn.Open()
MsgBox("Seccessfully connected to Mysql Database ")
' Dim cmd As New MySqlCommand(dt, conn)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = dt
.CommandType = CommandType.Text
.Connection = conn
.ExecuteNonQuery()
End With
conn.Close()
'MsgBox("SuccesFully")
conn.Dispose()
MsgBox("Port Setting OK")
Else
conn.Close()
'conn.Dispose()
MsgBox("Connecttion is Closed.")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
TextBox1.Text = cboshow & "connected"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'เมื่อมีการคลิกให้โหลดค่าที่ทำการupdateแล้วมาแสดงผล
Dim myCommand As New MySqlCommand
Dim myData As New DataTable 'ประกาศค่าสร้างตาราง
Dim Sql As String = "SELECT * FROM comport"
Dim myAdapter As New MySqlDataAdapter(Sql, str) 'ใส่คำสั่ง myslqlในการค้นหาข้อมูล strคือ การเชื่อมต่อฐานข้อมูล ,sql การquery ข้อมูล
myAdapter.Fill(myData) 'ผูก myadapter ที่เก็บคำสั่งค้นข้อมูลเข้ากับตารางที่ชื่อ mydata
' CboPort.DataSource = myData
With CboPort 'กำหนดค่าให้ combobox ที่ชื่อ CboPort
.DataSource = myData ' กำหนด datasource ให้กับ combobox ชื่อตารางmydata
.DisplayMember = myData.Columns.Item("Port").ColumnName 'แสดงผลในcomboboxโดย แสดงข้อมูลที่อยู่ใน ฟิวPortในตารางฐานข้อมูลที่ใช้
.SelectedItem = myData.Columns.Item("IdPort").ColumnName 'รหัส primary key ในตารางที่ใช้อ้างถึงข้อมูลที่นำมาแสดง
End With
With Cbobuad 'CboPort
.DataSource = myData
.DisplayMember = myData.Columns.Item("BitperSec").ColumnName
.SelectedItem = myData.Columns.Item("IdPort").ColumnName
End With
With CboBit
.DataSource = myData
.DisplayMember = myData.Columns.Item("Databit").ColumnName
.SelectedItem = myData.Columns.Item("IdPort").ColumnName
End With
With Cboparity
.DataSource = myData
.DisplayMember = myData.Columns.Item("Parity").ColumnName
.SelectedItem = myData.Columns.Item("IdPort").ColumnName
End With
With CboStb
.DataSource = myData
.DisplayMember = myData.Columns.Item("StopBits").ColumnName
.SelectedItem = myData.Columns.Item("IdPort").ColumnName
End With
End Sub
End Class