|
|
|
เมื่อกรอกข้อมูลลง textbox กดบันทึกข้อมูล แล้ว DataGridView ก็จะดึงข้อมูลใน Mysql มาแสดงใน DataGridView VB |
|
|
|
|
|
|
|
Dim objCmd As New MySqlCommand
strConnString = "Server=localhost;User Id=root; Password=1234; Database=themushrooms; Pooling=false"
strSQL = "INSERT INTO phase (Ph_nm,Ph_Date,Ph_Detail,Temp_max,Temp_min,Mous_max,Mous_min) " & _
" VALUES " & _
" ('" & Me.txt_namephase.Text.Trim & "','" & Me.txt_timework.Text.Trim & "','" & Me.txt_detailphase.Text.Trim & "','" & Me.txt_Temp_max.Text.Trim & "','" & Me.txt_Temp_min.Text.Trim & "','" & Me.txt_Mous_max.Text.Trim & "','" & Me.txt_Mous_min.Text.Trim & "')"
objConn.ConnectionString = strConnString
objConn.Open()
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Try
objCmd.ExecuteNonQuery()
MessageBox.Show("บันทึกเรียบร้อย")
Catch ex As Exception
MessageBox.Show("ไม่สามารถบันทึกได้ (" & ex.Message & ")")
End Try
objConn.Close()
objConn = Nothing
End Sub
Code (VB.NET)
Dim strConnString, strSQL As String
Dim objConn As New MySqlConnection
Dim ListFile() As String
Private Sub PopulateData()
'*** BindData From Database **'
Dim objConn As OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Server=localhost;User Id=root; Password=1234; Database=themushrooms;Pooling=false"
objConn = New OleDbConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM phase"
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Me.DataGridView1.DataSource = dt
'******************************'
Me.DataGridView1.Columns.Clear()
With DataGridView1.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(DataGridView1.Font, FontStyle.Bold)
End With
Dim ColumnTextBox As DataGridViewTextBoxColumn
'*** Column (Select) ***'
Dim ColumnCheckBox As New DataGridViewCheckBoxColumn()
ColumnCheckBox.Width = 30
ColumnCheckBox.DataPropertyName = "Select"
Me.DataGridView1.Columns.Add(ColumnCheckBox)
DataGridView1.Columns(0).Frozen = False
'*** Column (Ph_id) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Ph_id"
ColumnTextBox.HeaderText = "Ph_id"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (Ph_nm) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Ph_nm"
ColumnTextBox.HeaderText = "Ph_nm"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (Ph_Date) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Ph_Date"
ColumnTextBox.HeaderText = "Ph_Date"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (Ph_Detail) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Ph_Detail"
ColumnTextBox.HeaderText = "Ph_Detail"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (Temp_max) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Temp_max"
ColumnTextBox.HeaderText = "Temp_max"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (Temp_min) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Temp_min"
ColumnTextBox.HeaderText = "Temp_min"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (Mous_max) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Mous_max"
ColumnTextBox.HeaderText = "Mous_max"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (Mous_min) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Mous_min"
ColumnTextBox.HeaderText = "Mous_min"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
End Sub
Tag : MySQL, VB.NET
|
ประวัติการแก้ไข 2014-10-17 16:51:19 2014-10-17 16:52:08 2014-10-17 16:52:55 2014-10-17 16:53:54 2014-10-17 16:54:31
|
|
|
|
|
Date :
2014-10-17 16:46:54 |
By :
thaparnit |
View :
1063 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VB.NET + MySQL
|
|
|
|
|
Date :
2014-10-18 09:55:38 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|