 |
|
ขอสอบถามเรื่องการ insert 2 ตาราง มันบันทึกลงแค่ตารางเดียวครับ |
|
 |
|
|
 |
 |
|
คือ มันบันทึกลงแค่ตารางเดียว คือตาราง order_detail
อยากบันทึก 2 ตาราง แล้ว สองตารางนี้มีไอดีเชื่อมกันคือ Or_id
ตารางที่ 1 order_detail มี
Or_id, Printdata, Com_id
ตารางที่ 2 order มี
Or_id, Pro_id, Or_Quality
นี่ครับ Code
Code (VB.NET)
Private Sub process(ByVal st As String)
If dtpPrint.Text = "" Or txtComName.Text = "" Or txtComtel.Text = "" Or dgvshow.Rows.Count = 0 Then
MessageBox.Show("ข้อมูลไม่ครบ")
Exit Sub
End If
If st = "add" Then '--เลือก insert ลงตารางก่อนบันทึก
If MessageBox.Show("ต้องการบันทึกข้อมูลหรือไม่", "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Dim sql As String
For i As Integer = 0 To dgvshow.Rows.Count - 1
sql = "INSERT INTO order_detail (Or_id, Printdata, Com_id)" & _
"VALUES('" & Me.txtOrderid.Text & "','" & Me.dtpPrint.Text & "','" & Me.txtcomid.Text & "')"
Dim sql2 As String
sql2 = "INSERT INTO [order] (Or_id, Pro_id, Or_Quality)" & _
"VALUES('" & Me.txtOrderid.Text & "','" & Me.txtproid.Text & "','" & CInt(dgvshow.Rows(i).Cells(2).Value) & "')"
Try
Dim cm As New OleDbCommand(sql, Cnn)
Dim com As New OleDbCommand(sql2, Cnn)
cm.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
Next
MessageBox.Show("บันทึกข้อมูลเรียบร้อย", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
'ElseIf st = "edit" Then
' If MessageBox.Show("ต้องการแก้ไขข้อมูลหรือไม่", "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
' Dim sql As String
' sql = " update [order] set"
' sql &= " Printdate = '" & dtpPrint.Text & "',"
' sql &= " Pro_name = '" & txtProName.Text & "',"
' sql &= " Or_num= " & txtamount.Value & ","
' sql &= " Com_name= '" & txtComName.Text & "',"
' sql &= " [Tel]= '" & txtComtel.Text & "',"
' sql &= " Or_pt= " & CDbl(txtProPrice.Text)
' sql &= " Or_price= " & CDbl(txttotal.Text) & ","
' sql &= " where Or_id ='" & txtOrderid.Text & "'"
' Try
' Dim cm As New OleDbCommand(sql, Cnn)
' cm.ExecuteNonQuery()
' MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Information)
' Catch ex As Exception
' MessageBox.Show("ไม่สามารถแก้ไขข้อมูลได้", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
' End Try
' End If
'ElseIf st = "del" Then
' If MessageBox.Show("ต้องการลบข้อมูลหรือไม่", "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
' Dim sql As String
' sql = "delete from [order] where or_id ='" & txtOrderid.Text & "'"
' Try
' Dim cm As New OleDbCommand(sql, Cnn)
' cm.ExecuteNonQuery()
' MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Information)
' Catch ex As Exception
' MessageBox.Show("ไม่สามารถลบข้อมูลได้", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
' End Try
' End If
End If
'showdata()
'If ds.Tables("data").Rows.Count = 0 Then
Clear()
'Else
'detaildata(0)
'End If
End Sub
แล้วอีกอย่างอยากให้ตาราง order บันทึกแบบ 1 ID หลายเรคคอร์ดอย่างภาพ

Tag : .NET, Ms Access, VS 2012 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2016-02-24 13:26:46 |
By :
erlnw001 |
View :
1077 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Dim cm As New OleDbCommand(sql, Cnn)
Dim com As New OleDbCommand(sql2, Cnn)
cm.ExecuteNonQuery()
com.ExecuteNonQuery() หายไปไหนรึ
|
 |
 |
 |
 |
Date :
2016-02-24 13:35:06 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ง่ายสุดก็ for loop แบบบ้านๆครับ
Code (VB.NET)
for .......
sql2 = "INSERT INTO [order] (Or_id, Pro_id, Or_Quality)" & _
"VALUES('" & Me.txtOrderid.Text & "','" & Pro_id & "','" & Or_Quality & "')"
com.ExecuteNonQuery() 'Execute เพื่อ Insrt แต่ละเรคคอร์ด
Next
ขึ้นอยู่กับว่าไอ้เจ้า หลายๆ เรคคอร์ด ที่ว่านั้นมันอยู่ที่ไหนบ้างครับ
|
 |
 |
 |
 |
Date :
2016-02-24 14:35:13 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 3 เขียนโดย : lamaka.tor เมื่อวันที่ 2016-02-24 14:35:13
รายละเอียดของการตอบ ::
ขอโทษนะครับ คือยังบันทึกไม่ได้   ไม่แน่ใจว่า Code ถูกเปล่าครับ
นี่ครับ code
Code (VB.NET)
Private Sub process(ByVal st As String)
If dtpPrint.Text = "" Or txtComName.Text = "" Or txtComtel.Text = "" Or dgvshow.Rows.Count = 0 Then
MessageBox.Show("ข้อมูลไม่ครบ")
Exit Sub
End If
If st = "add" Then '--เลือก insert ลงตารางก่อนบันทึก
If MessageBox.Show("ต้องการบันทึกข้อมูลหรือไม่", "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Dim sql As String
For i As Integer = 0 To dgvshow.Rows.Count - 1
sql = "INSERT INTO order_detail (Or_id, Printdata, Com_id)" & _
"VALUES('" & Me.txtOrderid.Text & "','" & Me.dtpPrint.Text & "','" & Me.txtcomid.Text & "')"
For g As Integer = 0 To dgvshow.Rows.Count - 1
Dim sql2 As String
sql2 = "INSERT INTO [order] (Or_id, Pro_id, Or_Quality)" & _
"VALUES('" & Me.txtOrderid.Text & "','" & Me.txtproid.Text & "','" & CInt(dgvshow.Rows(i).Cells(2).Value) & "')"
Dim com As New OleDbCommand(sql2, Cnn)
com.ExecuteNonQuery()
Next
Try
Dim cm As New OleDbCommand(sql, Cnn)
cm.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
Next
MessageBox.Show("บันทึกข้อมูลเรียบร้อย", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
'ElseIf st = "edit" Then
' If MessageBox.Show("ต้องการแก้ไขข้อมูลหรือไม่", "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
' Dim sql As String
' sql = " update [order] set"
' sql &= " Printdate = '" & dtpPrint.Text & "',"
' sql &= " Pro_name = '" & txtProName.Text & "',"
' sql &= " Or_num= " & txtamount.Value & ","
' sql &= " Com_name= '" & txtComName.Text & "',"
' sql &= " [Tel]= '" & txtComtel.Text & "',"
' sql &= " Or_pt= " & CDbl(txtProPrice.Text)
' sql &= " Or_price= " & CDbl(txttotal.Text) & ","
' sql &= " where Or_id ='" & txtOrderid.Text & "'"
' Try
' Dim cm As New OleDbCommand(sql, Cnn)
' cm.ExecuteNonQuery()
' MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Information)
' Catch ex As Exception
' MessageBox.Show("ไม่สามารถแก้ไขข้อมูลได้", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
' End Try
' End If
'ElseIf st = "del" Then
' If MessageBox.Show("ต้องการลบข้อมูลหรือไม่", "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
' Dim sql As String
' sql = "delete from [order] where or_id ='" & txtOrderid.Text & "'"
' Try
' Dim cm As New OleDbCommand(sql, Cnn)
' cm.ExecuteNonQuery()
' MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Information)
' Catch ex As Exception
' MessageBox.Show("ไม่สามารถลบข้อมูลได้", "ผลการดำเนินการ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
' End Try
' End If
End If
'showdata()
'If ds.Tables("data").Rows.Count = 0 Then
Clear()
'Else
'detaildata(0)
'End If
End Sub
ขึ้น Error แบบนี้
|
 |
 |
 |
 |
Date :
2016-03-02 14:33:15 |
By :
erlnw001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยนเป็น
Code (VB.NET)
sql2 = "INSERT INTO [order] (Or_id, Pro_id, Or_Quality)" & _
"VALUES('" & CInt(Me.txtOrderid.Text) & "','" & CInt((Me.txtproid.Text) & "','" & CInt(dgvshow.Rows(i).Cells(2).Value) & "')"
เปลี่ยน string เป็น Int
|
 |
 |
 |
 |
Date :
2016-03-02 16:21:37 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|