Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'เพิ่มข้อมูลลงไปทีเดียวหลายเรคคอร์ด รูปแบบคำสั่งจะเป็นดังนี้
'INSERT INTO yourTable VALUES (row1col1, row1col2), (row2col1, row2col2), (row3col1, row3col2), (row4col1, row2col2)
'สมมุติมี 4 เรคคอร์ด และมี 2 คอลัมภ์
Dim strValues As String = ""
For i As Integer = 0 To 3 ' Array Zero base.
strValues &= "'StringValue'" & "12.00" & If(i < 3, ", ", "")
Next
Using cn As New SqlConnection("Data Source=YourPCName;Initial Catalog=YourDatabaseName;User ID=sa;Password=1234")
cn.Open()
Using ts As SqlTransaction = cn.BeginTransaction()
Try
Using cmd As New SqlCommand("INSERT INTO YourTableName VALUES (" & strValues & ")", cn)
cmd.Transaction = ts
cmd.ExecuteNonQuery()
ts.Commit()
End Using
Catch ex As Exception
ts.Rollback()
End Try
End Using
End Using
End Sub
End Class
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'เพิ่มข้อมูลลงไปทีเดียวหลายเรคคอร์ด รูปแบบคำสั่งจะเป็นดังนี้
'INSERT INTO yourTable VALUES (row1col1, row1col2), (row2col1, row2col2), (row3col1, row3col2), (row4col1, row2col2)
'สมมุติมี 4 เรคคอร์ด
Dim strValues As String = ""
For i As Integer = 0 To 3 ' Array Zero base.
strValues &= "(" & "'StringValue'" & "12.00" & If(i < 3, "), ", ")")
Next
Using cn As New SqlConnection("Data Source=YourPCName;Initial Catalog=YourDatabaseName;User ID=sa;Password=1234")
cn.Open()
Using ts As SqlTransaction = cn.BeginTransaction()
Try
Using cmd As New SqlCommand("INSERT INTO YourTableName VALUES " & strValues, cn)
cmd.Transaction = ts
cmd.ExecuteNonQuery()
ts.Commit()
End Using
Catch ex As Exception
ts.Rollback()
End Try
End Using
End Using
End Sub
End Class