Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim message As String = String.Empty
Dim cmd As New SqlCommand
Dim cn As New SqlConnection("Data Source=PTK530080085-PC;Initial Catalog=test;Integrated Security=True;Pooling=False")
cn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
If isSelected Then
message &= Environment.NewLine
message &= row.Cells("id").Value.ToString()
End If
Next
MessageBox.Show("Selected Values" & message)
End Sub
dim strsql as string
dim i as integer
for i = 0 to datagridview1.row.count - 1
dim val as string = datagridview1.row(i).cell(1).value.tostring() 'เก็บค่า รหัส ที่ต้องการลบ
if datagridview1.row(i).cell(o).formattedvalue then 'ที่มีการเช็ค
strsql="DELETE FROM[?] WHERE id='"& val &"'"
con.connection()
command = new sqlcommand(strsql,connection)
command.executenonquery
con.close()
end if
next
msgbox("ลบแล้วนะ")
private sub ExctSQL(strsql As string )
con.connection()
command = new sqlcommand(strsql,connection)
command.executenonquery
con.close()
end sub
การเอาไปใช้งาน
Code (VB.NET)
dim strsql as string
strsql="DELETE * FROM country WHERE CountryCode = '" & JP & "'" ' Delete
หรือ
strsql="INSERT INTO country VALUES ('CH','Chaina')" 'Insert
ExctSQL(strsql ) 'เรียกใช้งาน