使用数据集进行MsAccess数据库更新 [英] MsAccess Database Updation using Dataset

查看:67
本文介绍了使用数据集进行MsAccess数据库更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


我正在尝试使用数据集更新数据库,但似乎我错过了b
$ b。经过大量的帖子并检查我的代码后,我仍然在我的智慧结束时想出如何做一个简单的

更新。


这是代码。任何帮助将不胜感激。


问候,


Vibhu。


Dim conn As OleDbConnection

Dim da As OleDbDataAdapter

Dim ds As DataSet

Dim cb As OleDbCommandBuilder

Dim cm作为CurrencyManager


Private Sub Form5_Load(ByVal sender As Object,ByVal e As

System.EventArgs)处理MyBase.Load

conn = New OleDbConnection

conn.ConnectionString =" PROVIDER = MICROSOFT.JET.OLEDB.4.0;数据

SOURCE = C:\ceskpc1.mdb"

conn.Open()

da =新OleDbDataAdapter(" select * from tblCourseMaster",

conn)

cb =新OleDbCommandBuilder(da)

ds =新数据集

da。 Fill(ds," tblCourseMaster")

ds.Tables(0).Constraints.Add(" pk",ds.Tables(0).Columns(0),

True)

TextBox1.DataBindings.Add(" text",ds.Tables(0),

ds.Tables(0).Columns(0) .ToString)

TextBox2.DataBindings.Add(" text",ds.Tables(0),

ds.Tables(0).Columns(1).ToString )

cm = Me.BindingContext.Item(ds.Tables(0))

cb.GetUpdateCommand()

End Sub


Private Sub Button8_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button8.Click

试试

da.UpdateCommand = cb.GetUpdateCommand

MsgBox(da.UpdateCommand.CommandText)

da.Update(ds,& tblCourseMaster)

Catch ex As Exception

MsgBox(ex.Message)

结束尝试

结束Sub

Hello All,

I am trying to update a database using dataset but seems that I am
missing out something. After going through a lot of posts and checking
my code, I am still at my wits end to figure out how to do a simple
update.

Here is the code. Any help would be greatly appreciated.

Regards,

Vibhu.

Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim cb As OleDbCommandBuilder
Dim cm As CurrencyManager

Private Sub Form5_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
conn = New OleDbConnection
conn.ConnectionString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0; DATA
SOURCE=C:\ceskpc1.mdb"
conn.Open()
da = New OleDbDataAdapter("select * from tblCourseMaster",
conn)
cb = New OleDbCommandBuilder(da)
ds = New DataSet
da.Fill(ds, "tblCourseMaster")
ds.Tables(0).Constraints.Add("pk", ds.Tables(0).Columns(0),
True)
TextBox1.DataBindings.Add("text", ds.Tables(0),
ds.Tables(0).Columns(0).ToString)
TextBox2.DataBindings.Add("text", ds.Tables(0),
ds.Tables(0).Columns(1).ToString)
cm = Me.BindingContext.Item(ds.Tables(0))
cb.GetUpdateCommand()
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button8.Click
Try
da.UpdateCommand = cb.GetUpdateCommand
MsgBox(da.UpdateCommand.CommandText)
da.Update(ds, "tblCourseMaster")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

推荐答案

Vibhu,


您不使用类型化数据集(不是需要它)

但是,当你告诉它是*时,你怎么认为命令构建者得到关于

不同字段的信息。


我会在填充后执行命令构建器。


虽然我不会这样做getupdate命令。你已经把你的

dataadapter全局放了,所以它应该仍然存在。


这一切都是为了防止其他错误,你的主要问题是当<单击
按钮数据中的数据尚未推送。


强制您可以在按钮事件中设置为第一个语句。


BindingContext(ds.Tables(0))。EndCurrentEdit()


我希望这有帮助吗?


Cor
Vibhu,

You don''t use a typed dataset (not that it is needed)
However, how do you think that the commandbuilder gets it information about
the different fields when you have told that it are *.

I would do the commandbuilder after the fill.

While I would not do that getupdate command. You have placed your
dataadapter global, so that should be still there.

This all was to prevent other errors, your main problem is that when the
button is clicked the data is not pushed yet in the datatable.

To force that you can set as first statement in your button event.

BindingContext(ds.Tables(0)).EndCurrentEdit()

I hope this helps?

Cor


即使我更改了查询以反映列名并在填充后放入

命令构建器,它仍然没有更新数据库。然后

当我添加了BindingContext行时,它在调用更新的行

处出错。


另外,所有声明都是全局的,因此它们将可用。


类型''System.Data.OleDb.OleDbException''的第一次机会异常

发生在system.data.dll

Even after I changed the query to reflect the column names and put the
commandbuilder after fill, it still did not update the database. Then
when I added the BindingContext line, it gave an error at the line
where the update is called.

Also, all the declarations are global so they will be available.

A first chance exception of type ''System.Data.OleDb.OleDbException''
occurred in system.data.dll


另外,只是当我检查更新

命令的CommandText时的信息,我发现参数丢失,

值也是如此。如果这是一个问题,那么如何解决呢。

Also, just for information when I checked the CommandTextof the Update
command, I found that the parameters were missing and so were the
values. If this is a problem, then how can it be resolved.


这篇关于使用数据集进行MsAccess数据库更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆