Dim sqlconn As New SqlClient.SqlConnection
Dim myCommand As New SqlCommand
Dim dt As New DataTable
Dim i As Integer
If DataGridView1.Rows.Count <> 0 Then
'loop insert
For i = 0 To dt.Rows.Count - 1
Next
sqlconn.ConnectionString = "Data Source=BANK\SQLEXPRESS;Initial Catalog=HR;Integrated Security=True"
Dim sql As String = ""
sql = "INSERT INTO test (username,password,position)"
sql = sql & "VALUES ('" & DataGridView1.Rows(i).Cells(0).Value & "',"
sql = sql & "'" & DataGridView1.Rows(i).Cells(1).Value & "',"
sql = sql & "'" & DataGridView1.Rows(i).Cells(2).Value & "')"
With sqlconn
.ConnectionString = sqlconn.ConnectionString
.Open()
End With
With myCommand
.CommandType = Data.CommandType.Text
.CommandText = sql
.Connection = sqlconn
.ExecuteNonQuery()
End With
If MsgBox("บันทึกฐานข้อมูลเรียบร้อยแล้ว", CType(vbOKOnly + vbOKOnly, MsgBoxStyle), "ผลการทำงาน") Then
End If
End
sqlconn.Close()
sqlconn = Nothing
End If
Return '*** Return DataTable ***'
End Sub
Dim sqlconn As New SqlClient.SqlConnection
Dim myCommand As New SqlCommand
Dim dt As New DataTable
Dim i As Integer
If DataGridView1.Rows.Count <> 0 Then
'loop insert
For i = 0 To dt.Rows.Count - 1
sqlconn.ConnectionString = "Data Source=BANK\SQLEXPRESS;Initial Catalog=HR;Integrated Security=True"
Dim sql As String = ""
sql = "INSERT INTO test (username,password,position)"
sql = sql & "VALUES ('" & DataGridView1.Rows(i).Cells(0).Value & "',"
sql = sql & "'" & DataGridView1.Rows(i).Cells(1).Value & "',"
sql = sql & "'" & DataGridView1.Rows(i).Cells(2).Value & "')"
With sqlconn
.ConnectionString = sqlconn.ConnectionString
.Open()
End With
With myCommand
.CommandType = Data.CommandType.Text
.CommandText = sql
.Connection = sqlconn
.ExecuteNonQuery()
End With
Next ' เอาไว้ตรงนี้สิครับ
If MsgBox("บันทึกฐานข้อมูลเรียบร้อยแล้ว", CType(vbOKOnly + vbOKOnly, MsgBoxStyle), "ผลการทำงาน") Then
End If
End
sqlconn.Close()
sqlconn = Nothing
End If
Return '*** Return DataTable ***'
End Sub
มันชี้มาตรงนี้ sqlconn.ConnectionString = "Data Source=BANK\SQLEXPRESS;Initial Catalog=HR;Integrated Security=True" แล้ว เออเร่อ นะครับ
"Not allowed to change the 'ConnectionString' property. The connection's current state is open."