 |
|
ขอแนวทางการลบข้อมูลในตารางที่อยู่ใน database โดยที่ถ้าข้อมูลในตารางที่ 1 ตรงกับข้อมูลในตารางที่ 2 ก็ให้ลบข้อมูลในตารางที่ 2 ครับ |
|
 |
|
|
 |
 |
|
ขอแนวทางการลบข้อมูลในตารางที่อยู่ใน database เบื้องต้นผมได้ทำการเลือกข้อมูลใน ตารางที่ 2 เข้ามาในตารางที่ 1 ได้แล้ว หลังจากนั้นข้อมูลก็จะมีเหมือนกัน ก็ให้ลบข้อมูล ในตารางที่ 2 ทิ้งไปครับ
Code (VB.NET)
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date) values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
Dim start As Integer = dgv1.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv1.Rows(i).IsNewRow Then
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv1.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv1.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv1.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv1.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv1.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv1.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv1.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv1.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
End If
Next
'MessageBox.Show("คุณได้ทำการจองสินค้าเรียบร้อยแล้ว")
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try

Tag : .NET, VS 2012 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2017-04-22 10:59:58 |
By :
sakkapong |
View :
923 |
Reply :
13 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก็ไล่ลบตาม รหัส ไปได้เลยครับ
|
 |
 |
 |
 |
Date :
2017-04-22 11:23:35 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2017-04-22 12:07:09 |
By :
pakyaudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
If Me.DataGridView2.Rows.Count <> 0 Then
โค๊ตเดิมตอนที่แล้ว
Dim start As Integer = DataGridView2.CurrentRow.Index
For s As Integer = start To (start + CInt(Me.TextBox1.Text)) - 1
Dim r = DataGridView1.RowCount
DataGridView1.Rows.Add()
DataGridView1.Rows(r).Cells(0).Value = DataGridView2.Rows(s).Cells(0).Value
Next
'เพิ่มเติมลบตารางออก
For f As Integer = 0 To Me.DataGridView1.Rows.Count - 1
For g As Integer = 0 To Me.DataGridView2.Rows.Count - 1
If Me.DataGridView1.Rows(f).Cells(0).Value = Me.DataGridView2.Rows(g).Cells(0).Value Then
'ใส่ตรงนี้ก็ได้ครับ
sql = "DELETE FROM reservation WHERE id='" & Me.DataGridView2.Rows(g).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cn.open()
cmd.ExecuteNonQuery()
cn.close()
'จบส่วนลบในฐานข้อมูล
Me.DataGridView2.Rows.Remove(DataGridView2.Rows(g))
Exit For
End If
Next
Next
End If
ลบในฐานข้อมูลง่ายกว่าลบในดาต้ากริดอีกครับ
|
 |
 |
 |
 |
Date :
2017-04-22 15:43:00 |
By :
pakyaudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
https://www.thaicreate.com/community/csharp-datasource-wizard/view.html
จะลบจะเพิ่มเอาให้สะบายใจก่อนแล้วค่อยกด save
Code (C#)
private void sett_Post_PostCodeBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.sett_Post_PostCodeBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.postCodeDataSet);
}
มันจะลบที่เราลบไป แล้วจะเพิ่มไอ้ที่เราเพิ่มไป เว้ากันซื่อๆคือ มันจะอัพเดทที่เรากระทำไปตะกี้นั่นเองครับ
|
 |
 |
 |
 |
Date :
2017-04-22 16:17:19 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|


ตอนนี้ผมบรรทึกไม่ได้ครับ
1. ผมอยากเลือกข้อมูลจากตารางทางด้านขวา โดยจำนวนบรรทัดผมจะป้อนลงไปใน textbox เป็นตัวเลข เช่นต้องการแค่ 3 บรรทัดแรก ก็พิมพ์เลข 3
2. หลังจากกด button ok แล้ว ให้ทำการบันทึก ข้อมูลลง database แล้วดึงมาแสดงในตารางทางซ้ายมือ ซึ่งข้อมูลจะเหมือนกันทั้ง 2 ตาราง
แต่จะไม่เหมือนทั้งหมดเหมือนแค่ 3 บรรทัดที่ดึงมา
3. ถ้าตารางทางซ้ายมือ เหมือนกับตารางทางขวามือ ให้ลบข้อมูลในตารางด้านขวา
ข้อมูลผมผูกกับ database ครับ
|
 |
 |
 |
 |
Date :
2017-04-22 16:22:10 |
By :
sakkapong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ถูกนะครับ เพิ่มก่อนค่อยลบครับ
เช่น เพิ่ม 1 คำสั่ง
Code (VB.NET)
sql = "INSERT INTO reservation (xxx)VALUES(xxx);"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cn.open()
cmd.ExecuteNonQuery()
cn.close()
แล้วก็คอยมาลบอีก 1 คำสั่ง
Code (VB.NET)
sql = "DELETE FROM reservation WHERE id='" & Me.DataGridView2.Rows(g).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cn.open()
cmd.ExecuteNonQuery()
cn.close()
เช่น
Code (VB.NET)
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date) values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
Dim start As Integer = dgv1.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv1.Rows(i).IsNewRow Then
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv1.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv1.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv1.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv1.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv1.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv1.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv1.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv1.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
End If
Next
For f As Integer = 0 To Me.DataGridView1.Rows.Count - 1
For g As Integer = 0 To Me.DataGridView2.Rows.Count - 1
'ตรวจสอบว่าตารางรหัสของ Me.DataGridView1 เหมือนกับ Me.DataGridView2 ให้ลบตารางนั้นออก
If Me.DataGridView1.Rows(f).Cells(0).Value = Me.DataGridView2.Rows(g).Cells(0).Value Then
'ส่วนลบในฐานข้อมูล
sql = "DELETE FROM xxx WHERE id='" & Me.DataGridView2.Rows(g).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cn.open()
cmd.ExecuteNonQuery()
cn.close()
'จบส่วนลบในฐานข้อมูล
'ลบตารางในดาต้กริด DataGridView2
Me.DataGridView2.Rows.Remove(DataGridView2.Rows(g))
Exit For
End If
Next
Next
'MessageBox.Show("คุณได้ทำการจองสินค้าเรียบร้อยแล้ว")
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
ไม่ลบเฉพาะตารางก็โหลดใหม่จากฐานข้อมูลเลยก็ได้ครับ ข้อเสียการโหลดใหม่มาลงที่ ดาต้ากริด คือ ช้านิดหน่อยเมื่อปริมาณข้อมูลมีเยอะ
|
ประวัติการแก้ไข 2017-04-22 16:29:59 2017-04-22 16:32:22 2017-04-22 16:37:32
 |
 |
 |
 |
Date :
2017-04-22 16:29:29 |
By :
pakyaudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
open_database()
Try
If dgv02.Rows.Count <> 0 Then
Dim start As Integer = dgv02.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv02.Rows(i).IsNewRow Then
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date)" & _
"values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv02.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv02.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv02.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv02.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv02.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv02.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv02.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv02.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
End If
Next
End If
cn.Close()
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
If dgv02.Rows.Count > 0 Then
Dim start1 As Integer = dgv02.CurrentRow.Index
For i As Integer = start1 To (start1 + CInt(tb01.Text)) - 1
If Not dgv02.Rows(i).IsNewRow Then
sql &= "delete from in_stock where id='" & dgv02.Rows(start1).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
End If
Next
End If
load_dgv3()
load_dgv2()
End Sub
ผมทำแบบนี้บรรทึกได้แต่ลบไม่ได้ครับ ผมใส่อะไรผิดไปครับ
|
 |
 |
 |
 |
Date :
2017-04-22 16:43:19 |
By :
sakkapong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
open_database()
If dgv02.Rows.Count <> 0 Then
Dim start As Integer = dgv02.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv02.Rows(i).IsNewRow Then
Try
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date)" & _
"values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv02.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv02.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv02.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv02.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv02.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv02.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv02.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv02.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
sql &= "delete from in_stock where id='" & dgv02.Rows(start).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
Next
End If
cn.Close()
load_dgv3()
load_dgv2()
End Sub
ลองแบบนี้ดูครับ
|
ประวัติการแก้ไข 2017-04-22 17:35:53
 |
 |
 |
 |
Date :
2017-04-22 17:34:44 |
By :
pakyaudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2017-04-27 14:16:12 |
By :
sakkapong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมว่านะจะยังไม่ต้องลบสินค้าใน in_stock ออกนะครับ แต่เพิ่ม คอรั่มฐานข้อมูลอีก 1 อันที่กำหนดบอกว่าสินค้านี้จองแล้ว
ทีนี้เวลาจองสินค้าก็สั่งอับเดท ในตาราง in_stock ก่อน
ตัวอย่างเช่น reservations คือที่จะบอกว่าจองหรือยัง 1 คือ จองแล้ว 0 ยังไม่การจอง
Code (VB.NET)
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
open_database()
If dgv02.Rows.Count <> 0 Then
Dim start As Integer = dgv02.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv02.Rows(i).IsNewRow Then
Try
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date)" & _
"values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv02.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv02.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv02.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv02.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv02.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv02.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv02.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv02.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
sql &= "UPDATE in_stock SET reservations='1' where id='" & dgv02.Rows(start).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
Next
End If
cn.Close()
load_dgv3()
load_dgv2()
End Sub
ตรงโหลดข้อมูล load_dgv3() ก็สั่งให้โหลดเฉพาะ reservations='0' ด้วย ข้อมูลที่จองก็จะไม่โหลดมาด้วย
อาจจะเพิ่มปุ่ม บันทึกทั้งหมดด้วย และในปุ่มนี้ก็ลบ ใน in_stock ออก เลย
|
 |
 |
 |
 |
Date :
2017-04-27 17:19:42 |
By :
pakyaudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim start As Integer = dgv02.CurrentCell.RowIndex
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv02.Rows(i).IsNewRow Then
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date) values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
sql &= "delete from in_stock where product_id='" & dgv02.Rows(i).Cells(0).Value & "';"
'sql = "insert into in_stock(product_id, product_name, product_warehouse, product_amount, product_qty,product_lotno, product_color, product_date) values ('" & txtIDget.Text & "','" & txtPartget.Text & "','" & txtWhget.Text & "','" & txtAmountget.Text & "','" & txtQtyget.Text & "','" & txtLotget.Text & "','" & txtColorget.Text & "','" & DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") & "')"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv02.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv02.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv02.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv02.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv02.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv02.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv02.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv02.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
End If
Next
cn.Close()
ทำได้แล้วครับผมทำแบบนี้ ขอบคุณสำหรับทุกคำตอบครับ
|
 |
 |
 |
 |
Date :
2017-04-28 12:56:28 |
By :
sakkapong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|