 |
|
[VB.NET + MySQL] insert ข้อมูลลง 2 Table พร้อมกันไม่ได้ รบกวนผู้รู้ช่วยแนะนำทีครับ ผมมือใหม่(มาก)หัดเขียน >< |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Option Explicit On
Option Strict On
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class member
Dim MySQLConn As MySqlConnection
Dim da As MySqlDataAdapter
Dim ds As DataSet
Dim tb As DataTable
Dim StrSQL As String
'ส่วนประกาศตัวแปรไว้เชื่อมต่อกับฐานข้อมูล
Private Sub member_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
MySQLConn = New MySqlConnection
MySQLConn.ConnectionString = "Server=localhost; Database=bookrental; Uid=JN; Pwd=12345;charset=utf8"
MySQLConn.Open()
If MySQLConn.State = ConnectionState.Open Then
MySQLConn.Open() 'ให้เปิดฟอร์มเลย แต่ถ้าอยากให้มีการแสดงข้อความก่อนก็ -->'MsgBox("Connected")
Call ShowData()
Else
MsgBox("Error")
End If
Catch ex As Exception
End Try
Call ShowData()
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.RowIndex = -1 Then Exit Sub
With DataGridView1
txtIDMem.Text = .Rows.Item(e.RowIndex).Cells(0).Value.ToString()
cboTitle.Text = .Rows.Item(e.RowIndex).Cells(1).Value.ToString()
txtName.Text = .Rows.Item(e.RowIndex).Cells(2).Value.ToString()
txtLName.Text = .Rows.Item(e.RowIndex).Cells(3).Value.ToString()
txtIDCard.Text = .Rows.Item(e.RowIndex).Cells(4).Value.ToString()
txtAddress.Text = .Rows.Item(e.RowIndex).Cells(5).Value.ToString()
txtPNumber.Text = .Rows.Item(e.RowIndex).Cells(6).Value.ToString()
txtEMail.Text = .Rows.Item(e.RowIndex).Cells(7).Value.ToString()
'เวลาคลิกแล้วจะโชว์ข้อมูลเพื่อแก้ไข
End With
End Sub
Public Sub formatGrid() ' ฟังค์ชันเปลี่ยนชื่อ column ใน DataGrid
With DataGridView1
.Columns(0).HeaderText = "รหัสสมาชิก" '<==(ชื่อที่ต้องการเปลี่ยน)
.Columns(1).HeaderText = "คำนำหน้าชื่อ"
.Columns(2).HeaderText = "ชื่อ"
.Columns(3).HeaderText = "นามสกุล"
.Columns(4).HeaderText = "รหัสประชาชน"
.Columns(5).HeaderText = "ที่อยู่สมาชิก"
.Columns(6).HeaderText = "เบอร์โทรสมาชิก"
.Columns(7).HeaderText = "E-Mail"
.Columns(0).Width = 130 '<==(ขนาดความกว้าง)
.Columns(1).Width = 100
.Columns(2).Width = 100
.Columns(3).Width = 80
.Columns(4).Width = 122
.Columns(5).Width = 100
.Columns(6).Width = 100
.Columns(7).Width = 80
End With
End Sub
Sub clearData()
txtIDMem.Clear()
cboTitle.Text = "คำนำหน้าชื่อ:" 'แสดงเมื่อล้างค่า
txtName.Clear()
txtLName.Clear()
txtIDCard.Clear()
txtAddress.Clear()
txtPNumber.Clear()
txtEMail.Clear()
'ล้างข้อมูลให้เป็นช่องว่าง
End Sub
Sub ShowData()
ds = New DataSet
StrSQL = "SELECT m.Mem_ID,m.Mem_Title,m.Mem_FName,m.Mem_LName,m.Mem_IDCard,m.Mem_Address,t.Mem_Phone,m.Mem_Mail FROM member m INNER JOIN talephone_member t ON m.Mem_IDCard=t.Mem_IDCard"
da = New MySqlDataAdapter(StrSQL, MySQLConn)
da.Fill(ds, "member")
DataGridView1.DataSource = ds.Tables("member")
Call formatGrid()
'SET ค่า DataGrid
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
If MessageBox.Show("คุณต้องการเพิ่มข้อมูลนี้ใช่หรือไม่ ?", "ยืนยันการเพิ่มข้อมูลรายการเมนูอาหาร", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
ds = New DataSet
StrSQL = "INSERT INTO member(Mem_ID,Mem_Title,Mem_FName,Mem_LName,Mem_IDCard,Mem_Address,Mem_Mail) " & " VALUES ('" & txtIDMem.Text.Trim() & "', '" & cboTitle.Text.Trim() & "', '" & txtName.Text.Trim() & "', '" & txtLName.Text.Trim() & "' , '" & txtIDCard.Text.Trim() & "', '" & txtAddress.Text.Trim() & "' , '" & txtEMail.Text.Trim() & "')" & "INSERT INTO talephone_member(Mem_Phone) VALUES ('" & txtPNumber.Text.Trim() & "')"
da = New MySqlDataAdapter(StrSQL, MySQLConn)
da.Fill(ds, "member")
MessageBox.Show("เพิ่มข้อมูลเรียบร้อย", "เสร็จสิ้น", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call ShowData()
Call clearData()
End If
End Sub
End Class
อันนี้เป็น From Member ครับ มีตารางที่ Join กัน 2 ตารางคือ member กับ talephone_member อยากจะ insert ข้อมูลลง MySQL ทั้งสองตารางพร้อมกัน รบกวนผู้รู้แนะนำหน่อยครับ ขอบคุณมากๆ ครับ
Tag : MySQL, Win (Windows App), VB.NET, VS 2012 (.NET 4.x)
|
ประวัติการแก้ไข 2015-08-31 19:45:46
|
 |
 |
 |
 |
Date :
2015-08-31 19:42:35 |
By :
Jamesillion |
View :
4043 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Insert ทั้งสองตาราง พร้อมๆ กันซับซ้อนครับ แยก Insert ทีละตารางครับ ได้ผลลัพท์เท่ากับคือ Insert ตาราง Member ให้ได้ก่อน จากนั้นค่อย Insert ตาราง talephone_member ครับ
|
 |
 |
 |
 |
Date :
2015-09-01 09:22:01 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim objConn As New MySqlConnection
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
objConn.Open()
Dim strConnString,strSQL As String
Dim objCmd1 As New MySqlCommand
'*** Insert 1
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
" VALUES " & _
" ('" & Me.txtCustomerID.Text & "','" & Me.txtName.Text & "','" & Me.txtEmail.Text & "', " & _
" '" & Me.txtCountryCode.Text & "','" & Me.txtBudget.Text & "','" & Me.txtUsed.Text & "')"
objConn.ConnectionString = strConnString
With objCmd1
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
.ExecuteNonQuery()
End With
'*** Insert 2
Dim objCmd2 As New MySqlCommand
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
" VALUES " & _
" ('" & Me.txtCustomerID.Text & "','" & Me.txtName.Text & "','" & Me.txtEmail.Text & "', " & _
" '" & Me.txtCountryCode.Text & "','" & Me.txtBudget.Text & "','" & Me.txtUsed.Text & "')"
objConn.ConnectionString = strConnString
With objCmd2
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
.ExecuteNonQuery()
End With
เขียน Insert แบบ 2 Statement อันนี้ง่าย ๆ Basic ครับ
|
 |
 |
 |
 |
Date :
2015-09-01 09:34:11 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณท่านมากๆ ครับ ตอนนี้ทำได้แล้วครับผม :)
|
 |
 |
 |
 |
Date :
2015-09-05 14:12:56 |
By :
Jamesillion |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2015-09-05 17:49:15 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|