如何更新MS Access数据库 [英] how to update ms access database
问题描述
有人可以指导我如何更新ms Access数据库中的某些记录吗...
我有一个称为联系人"的表,这些字段是:
联系人ID,名字,姓氏,电话号码,地址,电子邮件等
现在在我的vb.net表单上,我有一个名为update的按钮,当按下该按钮时,我希望为该记录更新输入到文本框中的文本.
例如,我想更新联系人ID 1的地址,我该怎么做?
我花了4个多小时来尝试实现这一目标,但我一直遇到错误..
请帮忙!
谢谢
can someone please guide me on how i can update certain records in my ms access database...
i have a table called contacts and the fields are:
contact id, first name, last name, phone number, address, e-mail etc
now on my vb.net form i have a button called update and when that is pressed i want the text enetered into the textbox to be update for that record.
for e.g i wish to update the address of contact id 1, how can i do this??
i have spent over 4 hours trying to achieve this but i keep getting errors..
please help!
thanks
推荐答案
首先,您应该将此代码放在代码的上部,就在您使用的表单的类之前
first you should put this code on the upper part of the codes just before the class of the form that you are using
Imports System.Data.OleDb 'this would be the declaration of OLEDB namespace, this might be the reason why you are having blue line for OleDBConnection and OleDbDataAdapter because the namespace that handles this codes is not declared
Dim con as new OleDBConnection
Dim ds As New DataSet
Dim da As New OleDbDataAdapter
con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & My.Application.Info.DirectoryPath & "\dbase.mdb" 'dbase.mdb is the database file that you are using
da = New OleDbDataAdapter("select * from contacts where [contact id] = '" & Textbox1.text & "'", reservationConn) 'get the record based on the textbox containing the ID of the person that you want to edit
da.Fill(ds, "Contacts")
inc=-1
ds.Tables("s").Rows(inc).Item(1) = TextBox2.Text
ds.Tables("s").Rows(inc).Item(2) = TextBox3.Text
ds.Tables("s").Rows(inc).Item(3) = TextBox4.Text
ds.Tables("s").Rows(inc).Item(4) = TextBox5.Text
ds.Tables("s").Rows(inc).Item(5) = TextBox6.Text
ds.Tables("s").Rows(inc).Item(6) = TextBox7.Text
ds.Tables("s").Rows(inc).Item(7) = TextBox8.Text
ds.Tables("s").Rows(inc).Item(8) = TextBox9.Text
da.Update(ds, "s")
MsgBox("updated")
*注意
您可以尝试此代码.希望对您有所帮助!
*Note
You can try this code. Hope it helps!
这篇关于如何更新MS Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!