插入到OLEDB VB.NET [英] Insert into OLEDB VB.NET

查看:137
本文介绍了插入到OLEDB VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有什么问题?

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

数据库从不更新。
或者有更好的方法插入db?我试过CommandBuilder,但它似乎没有工作,以及。
或者我可以直接在VB.NET中对我的数据库执行查询?

解决方案

在什么DataAdapter.Update。
更新工作将应用您的InsertCommand,UpdateCommand,DeleteCommand到DataSource中的每个添加,修改或删除行(假设ds是DataSet)。

它不添加/删除/更新



如果要向数据库中添加一条记录,则应该像这样写入(伪)代码



使用con = GetConnection()
使用cmd = new con.CreateCommand()
cmd.CommandText =Insert Into Table1 Values ,?)
cmd.Parameters.AddWithValue(@ param1,textbox1.Text)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery
结束使用
结束使用

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



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

 使用con = GetConnection()
使用cmd = new con.CreateCommand()
cmd .CommandText =Insert Into Table1 Values(?)
cmd.Parameters.AddWithValue(@ param1,textbox1.Text)
cmd.Connection = con
con.Open b $ b cmd.ExecuteNonQuery()
结束使用
结束使用

,对于'没有添加'的问题我认为你有数据库文件(mdb)包括在您的项目和属性复制到输出目录设置为复制始终



查看此页面上的MSDN


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?

解决方案

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.

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.

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

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 Directory set to Copy Always

See a detailed explanation on MSDN at this page

这篇关于插入到OLEDB VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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