从 gridview 获取数据到 access 数据库
getting data from a gridview into an access database
我大部分时间都在使用它,但我无法将所有 gridview 数据导入数据库,它只获取第一行数据,这里是代码:
Try
Dim pid As String
Dim pname As String
Dim Connection As OleDbConnection
Connection = New OleDbConnection()
Dim connolaString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/my_products.mdb;") + ";Persist Security Info=True"
Connection.ConnectionString = connolaString
Dim commander As OleDbCommand = New OleDbCommand()
commander.Connection = Connection
commander.CommandText = "INSERT INTO prdtemp ( pid, pname ) VALUES ( @pid, @pname )"
Connection.Open()
For x As Integer = 0 To GridView1.Rows.Count - 1
pid = GridView1.Rows(x).Cells(1).Text
pname = GridView1.Rows(x).Cells(2).Text
commander.Parameters.AddWithValue("@pid", pid)
commander.Parameters.AddWithValue("@pname", pname)
commander.ExecuteNonQuery()
commander.Dispose()
Next
Connection.Close()
Catch ex As Exception
lblMessage.Text = (ex.Message)
End Try
出于某种原因,(ex.message) 表示连接 属性 未初始化,尽管就像我说的那样它仅获取 gridview 的第一行。请帮忙。
您似乎在循环中调用了 commander.Dispose()
方法。这将释放所有对象的资源并在你的第二次循环迭代中破坏事物。
编辑:我建议你用 Using
语句包装 OleDbCommand
,当它退出 Using
块时,它会为你处理对象处理。
我没有测试下面修改后的代码,但它应该可以解决你的问题。
Try
Dim pid As String
Dim pname As String
Dim Connection As OleDbConnection
Connection = New OleDbConnection()
Dim connolaString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/my_products.mdb;") + ";Persist Security Info=True"
Using commander As New OleDbCommand(connolaString)
commander.Connection = Connection
commander.CommandText = "INSERT INTO prdtemp ( pid, pname ) VALUES ( @pid, @pname )"
Connection.Open()
For x As Integer = 0 To GridView1.Rows.Count - 1
pid = GridView1.Rows(x).Cells(1).Text
pname = GridView1.Rows(x).Cells(2).Text
commander.Parameters.Clear()
commander.Parameters.AddWithValue("@pid", pid)
commander.Parameters.AddWithValue("@pname", pname)
commander.ExecuteNonQuery()
Next
Connection.Close()
End Using
Catch ex As Exception
lblMessage.Text = (ex.Message)
End Try
我大部分时间都在使用它,但我无法将所有 gridview 数据导入数据库,它只获取第一行数据,这里是代码:
Try
Dim pid As String
Dim pname As String
Dim Connection As OleDbConnection
Connection = New OleDbConnection()
Dim connolaString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/my_products.mdb;") + ";Persist Security Info=True"
Connection.ConnectionString = connolaString
Dim commander As OleDbCommand = New OleDbCommand()
commander.Connection = Connection
commander.CommandText = "INSERT INTO prdtemp ( pid, pname ) VALUES ( @pid, @pname )"
Connection.Open()
For x As Integer = 0 To GridView1.Rows.Count - 1
pid = GridView1.Rows(x).Cells(1).Text
pname = GridView1.Rows(x).Cells(2).Text
commander.Parameters.AddWithValue("@pid", pid)
commander.Parameters.AddWithValue("@pname", pname)
commander.ExecuteNonQuery()
commander.Dispose()
Next
Connection.Close()
Catch ex As Exception
lblMessage.Text = (ex.Message)
End Try
出于某种原因,(ex.message) 表示连接 属性 未初始化,尽管就像我说的那样它仅获取 gridview 的第一行。请帮忙。
您似乎在循环中调用了 commander.Dispose()
方法。这将释放所有对象的资源并在你的第二次循环迭代中破坏事物。
编辑:我建议你用 Using
语句包装 OleDbCommand
,当它退出 Using
块时,它会为你处理对象处理。
我没有测试下面修改后的代码,但它应该可以解决你的问题。
Try
Dim pid As String
Dim pname As String
Dim Connection As OleDbConnection
Connection = New OleDbConnection()
Dim connolaString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/my_products.mdb;") + ";Persist Security Info=True"
Using commander As New OleDbCommand(connolaString)
commander.Connection = Connection
commander.CommandText = "INSERT INTO prdtemp ( pid, pname ) VALUES ( @pid, @pname )"
Connection.Open()
For x As Integer = 0 To GridView1.Rows.Count - 1
pid = GridView1.Rows(x).Cells(1).Text
pname = GridView1.Rows(x).Cells(2).Text
commander.Parameters.Clear()
commander.Parameters.AddWithValue("@pid", pid)
commander.Parameters.AddWithValue("@pname", pname)
commander.ExecuteNonQuery()
Next
Connection.Close()
End Using
Catch ex As Exception
lblMessage.Text = (ex.Message)
End Try