如何使用 vb.net 将数据插入 Access 表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/337333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:44:36  来源:igfitidea点击:

How would I insert data into an Access table using vb.net?

.netvb.netms-accessinsert

提问by seanyboy

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

我想在 Access 数据库中插入一个新行。我正在考虑做类似的事情:

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.

这对我来说似乎是一种明智的做事方式。

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.

但是,我在网上寻找的所有示例似乎都是通过构建 SQL 语句或通过创建“SELECT * From ...”来插入数据,然后使用它来创建多个对象,其中一个似乎允许您to ...
- 用表的当前内容填充数组。
- 在这个数组中插入一个新行。
- 使用对数组的更改更新数据库。

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?

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

采纳答案by Stefan

This is one way:

这是一种方式:

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

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):

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

    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)

回答by Stefan

how to insert the string in vb.net only insert in the integer my pgm is

如何在 vb.net 中插入字符串只插入我的 pgm 是的整数

Try
    cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\User\My Documents\db1.mdb;")
    cn.Open()
    str = "insert into table1 values(" & CInt(t2.Text) & ",'" & (t1.Text) & ") "
    cmd = New OleDbCommand(str, cn)
    icount = cmd.ExecuteNonQuery
    MessageBox.Show("stored")
Catch
End Try
cn.Close()

回答by Joel Coehoorn

Unfortunately, the world doesn't re-arrange itself to suit how you think it should work. If you want to work with a database, it's a good idea to get at least somewhat comfortable with SQL, as that is generally how it's done.

不幸的是,世界不会重新安排自己以适应您认为它应该如何运作。如果您想使用数据库,那么至少熟悉 SQL 是个好主意,因为它通常是这样完成的。

On the bright side, there is a whole category of products that do kind of what you want. You can look for an Object-Relational Mappings (ORM) tool like ActiveRecord, NHibernate, or LINQ. But still learn some SQL.

从好的方面来说,有一整套产品可以满足您的需求。您可以寻找对象关系映射 (ORM) 工具,例如 ActiveRecord、NHibernate 或 LINQ。不过还是学了一些SQL。