VB.net2008 通过Gridview 向Access数据库插入记录~
Imports System.Data.OleDb
Public Class Form1
Dim myconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database1.mdb"
Dim cn As OleDbConnection = New OleDbConnection(myconn)
Private Dset As DataSet
Dim ODA1 As OleDbDataAdapter
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
MsgBox("确定要退出吗", MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "提示信息")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strsql As String
Dim oda1 As New OleDbDataAdapter()
cn.Open()
Dim objDA As OleDbCommand = New OleDbCommand(strsql, cn)
Console.Write(strsql)
objDA.CommandText = "insert into [YLQWP] (ID,CTBH,LX) values('" & DataGridView1.Rows(0).Cells(0).Value & "','" & DataGridView1.Rows(0).Cells(1).Value & "','" & DataGridView1.Rows(0).Cells(2).Value & "')"
objDA.ExecuteNonQuery()
Dset = New DataSet()
oda1.Fill(Dset)//报错点!!!错误为(在调用“Fill”前,SelectCommand 属性尚未初始化。)
cn.Close()
End Sub
请问给位高人如何解决这个问题~小弟苦思很久不得解决~跪求一良方~
[解决办法]
[解决办法]
- VB.NET code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database1.mdb" Dim cn As OleDbConnection = New OleDbConnection(myconn) cn.open Dim strsql As String Dim OleCommand As OleDbCommand = New OleDbCommand OleCommand.Connection=Cn OleCommand.CommandText="select ID,CTBH,LX From YLQWP" Dim oda1 As New OleDbDataAdapter(OleCommand) Dset = New DataSet() oda1.Fill(Dset) cn.Close() DataGridView1.DataSource=Dset.table(0) End Sub