插入 OLEDB VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13932095/
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-17 11:35:17  来源:igfitidea点击:

Insert into OLEDB VB.NET

databasevb.netinsertoledb

提问by v4g

What is wrong with this?

这有什么问题?

da.InsertCommand = New OleDb.OleDbCommand("Insert Into Table1 Values( 1, '" & TextBox1.Text & "')")
da.InsertCommand.Connection = con
con.Open()
da.Update(ds)
con.Close()

The database is never updated no matter what. Or is there a better way to insert into db? I have tried the CommandBuilder but it doesnt seem to work as well. Or can I directly execute a query on my database in VB.NET?

无论如何,数据库永远不会更新。或者有更好的方法插入数据库吗?我已经尝试过 CommandBuilder,但它似乎也不起作用。或者我可以直接在 VB.NET 中对我的数据库执行查询吗?

回答by Steve

You are a bit confused regarding on what DataAdapter.Update does. The Update works applying your InsertCommand, UpdateCommand, DeleteCommand to every Added, Modified or Deleted rows present in your DataSource (supposing ds is a DataSet).
It doesn't add/delete/update a record to the database by itself.

您对 DataAdapter.Update 的作用有点困惑。更新工作将您的 InsertCommand、UpdateCommand、DeleteCommand 应用于数据源中存在的每个添加、修改或删除的行(假设 ds 是数据集)。
它本身不会向数据库添加/删除/更新记录。

If you want to add a record to the database you should write (pseudo)code like this

如果你想向数据库添加一条记录,你应该像这样编写(伪)代码

Using con = GetConnection()
Using cmd = new con.CreateCommand()
    cmd.CommandText = "Insert Into Table1 Values( 1, ?)"
    cmd.Parameters.AddWithValue("@param1", textbox1.Text)
    cmd.Connection = con
    con.Open()
    cmd.ExecuteNonQuery()
End Using
End Using

Note the use of the Using statement to be sure of closing and disposing the connection and the command. Also, never concatenate strings to build sql commands. Using parameters avoid parsing problems and SQLInjection attacks.

请注意使用 Using 语句以确保关闭和处理连接和命令。此外,永远不要连接字符串来构建 sql 命令。使用参数可以避免解析问题和 SQLInjection 攻击。

EDIT: Based on your comment below and supposing that the first column of the Table1 is an autonumber field you could change the code in this way

编辑:根据您在下面的评论并假设 Table1 的第一列是自动编号字段,您可以通过这种方式更改代码

Using con = GetConnection()
Using cmd = new con.CreateCommand()
    cmd.CommandText = "Insert Into Table1 Values(?)"
    cmd.Parameters.AddWithValue("@param1", textbox1.Text)
    cmd.Connection = con
    con.Open()
    cmd.ExecuteNonQuery()
End Using
End Using

Also, for the problem for 'nothing has been added' I think you have the database file (mdb) included in your project and the property Copy To The Output Directoryset to Copy Always

另外,对于“没有添加任何内容”的问题,我认为您的项目中包含了数据库文件 (mdb) 并且属性Copy To The Output Directory设置为Copy Always

See a detailed explanation on MSDN at this page

在此页面上查看MSDN 上的详细说明

回答by SHAKIR SHABBIR

Yes, its because you havent tried to run your non-query. Remember, SELECT statement is a query statement while INSERT, UPDATE, DELETE are non-query statement. da.Update() or da.Fill() will only pull result from a query operation.

是的,这是因为您还没有尝试运行您的非查询。请记住,SELECT 语句是查询语句,而 INSERT、UPDATE、DELETE 是非查询语句。da.Update() 或 da.Fill() 只会从查询操作中提取结果。

You need to add an extra line after you open the connection as da.ExecuteNonQuery() or any statement that executes a non-query statement in VB.NET!

打开连接后,您需要添加额外的行作为 da.ExecuteNonQuery() 或在 VB.NET 中执行非查询语句的任何语句!

~Shakir Shabbir

~沙基尔沙比尔

回答by Ciarán

Neat and tidy...

干净整洁...

Using oCmd As New OleDbCommand("Insert Into Table1 Values(1, '" & TextBox1.Text & "')", con)
    oCmd.ExecuteNonQuery()
End Using

It's a bit nasty because of the possibility of SQL Injection or Textbox1.Text containing single quotes but for test/dev purposes it'll work.

由于 SQL 注入或 Textbox1.Text 包含单引号的可能性,这有点令人讨厌,但出于测试/开发目的,它会起作用。