如何使用vb.net将数据插入Access表中? [英] How would I insert data into an Access table using vb.net?

查看:107
本文介绍了如何使用vb.net将数据插入Access表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Access数据库中插入新行.我正在做类似的事情:

I want to insert a new row into an Access database. I'm looking at doing something like:

oConnection = new Connection("connectionstring")
oTable = oCennection.table("Orders")
oRow = oTable.NewRow
oRow.field("OrderNo")=21
oRow.field("Customer") = "ABC001"
oTable.insert

这似乎是对我做事的明智方式.

Which seems to be a sensible way of doing things to me.

但是,我在网上寻找的所有示例似乎都通过构建SQL语句或创建"SELECT * From ..."来插入数据,然后使用它来创建多个对象,其中一个出现让你...
-使用表的当前内容填充数组.
-在此数组中插入新行.
-使用对阵列所做的更改来更新数据库.

However, All examples I look for on the net seem to insert data by building SQL statements, or by creating a "SELECT * From ...", and then using this to create a multitude of objects, one of which appears to allow you to ...
- populate an array with the current contents of the table.
- insert a new row into this array.
- update the database with the changes to the array.

使用vb.net将数据插入Access数据库的最简单方法是什么?
有没有可以使用的方法与上面的pCode相似?

What's the easiest way of using vb.net to insert data into an Access database?
Is there a method I can use that is similar to my pCode above?

推荐答案

这是一种方法:

cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;")
cn.Open()
str = "insert into table1 values(21,'ABC001')"
cmd = New OleDbCommand(str, cn)
cmd.ExecuteNonQuery

我将创建一个数据集,添加一个连接到Access数据库的tableadapter,然后让tableadapter为我创建更新/删除/修改.然后,您可以这样做(假设您的数据库有一个用户表,并将其映射到数据集中):

I would make a dataset, add a tableadapter connected to the Access database, then let the tableadapter create update/delete/modify for me. Then you just could do like this (assuming your database has a usertable and you mapped that up in the dataset):

    Dim UserDS As New UserDS
    Dim UserDA As New UserDSTableAdapters.UsersTableAdapter
    Dim NewUser As UserDS.UsersRow = UserDS.Users.NewUsersRow

    NewUser.UserName = "Stefan"
    NewUser.LastName = "Karlsson"

    UserDS.User.AddUserRow(NewUser)

    UserDA.Update(UserDS.Users)

这篇关于如何使用vb.net将数据插入Access表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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