首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

Datagridview连接access的更新数据库有关问题

2012-09-06 
Datagridview连接access的更新数据库问题小弟我又遇上麻烦啦。。还是Datagridview。。可以实现从 Access DB里

Datagridview连接access的更新数据库问题
小弟我又遇上麻烦啦。。还是Datagridview。。
可以实现从 Access DB里面retrieve data
但是update有问题。说是语句有问题。。
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: UPDATE 语句的语法错误。

什么问题呢?

VB.NET code
Imports System.Data.OleDbPublic Class Form1    Dim con As OleDbConnection    Dim cmd As OleDbCommand    Dim myDA As OleDbDataAdapter    Dim myDataSet As DataSet    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=|DataDirectory|\Database2.mdb")        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM TimePicker", con)        con.Open()        myDA = New OleDbDataAdapter(cmd)        'One CommandBuilder object is required. It automatically generates DeleteCommand,UpdateCommand and InsertCommand for DataAdapter object              Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)        myDataSet = New DataSet()        myDA.Fill(myDataSet, "MyTable")        DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView        con.Close()        con = Nothing    End Sub    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click        Me.Validate()        Me.myDA.Update(Me.myDataSet.Tables("MyTable"))        Me.myDataSet.AcceptChanges()    End SubEnd Class 



[解决办法]
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'将这句写在这儿试试
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)

Me.Validate()
Me.myDA.Update(Me.myDataSet.Tables("MyTable"))
Me.myDataSet.AcceptChanges()
End Sub

[解决办法]
TimePicker表中必须要Key,
将source=改成绝对路径试一下,如source=E:\VB.net 2005\myAccessTable.mdb
con.Open() con.Close() con=Nothing可以不要。
可以看看Builder.GetUpdateCommand.CommandText中的SQL语名是否构建正确。
其它好象没什么错。

热点排行