 |
|
ไม่สามารถ วนลูป insert ได้ครับ ช่วยดูทีครับ คือผมเซฟข้อมูลจาก gridview มาลง database อะครับ |
|
 |
|
|
 |
 |
|
Code (ASP)
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
|
 |
 |
 |
 |
Date :
2009-12-02 14:34:18 |
By :
Sek-Artdrinker |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันชี้มาตรงนี้ 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."
|
 |
 |
 |
 |
Date :
2009-12-02 14:54:41 |
By :
songpons |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้ว ครับ ขอบคุณมากๆ ครับพี่
|
 |
 |
 |
 |
Date :
2009-12-02 15:03:57 |
By :
songpons |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|