如何将数据从VB添加和更新到MSAccess [英] How to add and update data from VB to MSAccess

查看:81
本文介绍了如何将数据从VB添加和更新到MSAccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了sandeep207的建议(放置Try / Catch),但它仍然不会添加/更新。



I tried sandeep207''s suggestion (of putting Try/Catch) but it still won''t add/update.

connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\invData.mdb;"
       cnn = New OleDbConnection(connectionString)
           Dim sql As String = "INSERT INTO NTIdb(ID, Status, DateDeliver, Client, POrder, StoreCode, StoreType, ProdUsage, ProdCategory, Brand, Description, Quantity, MainUnitSerial, HDD1, HDD2, OpticalDrive, Monitor, Keyboard, NetAdaptor, ProdKey, WTYEnt, AccExec, SalesOrder, SalesInv, DReceipt, ReceivedBy, WayBill, DueDate) " & _
           " VALUES(" & TextBox1.Text & ",'" & ComboBox1.SelectedItem() & "','" & _
                      DateTimePicker1.Value() & "','" & TextBox2.Text & "','" & _
                      TextBox3.Text & "', '" & TextBox4.Text & "', '" & TextBox5.Text & "','" & _
                      TextBox6.Text & "', '" & TextBox7.Text & "', '" & TextBox8.Text & "','" & _
                      TextBox9.Text & "', '" & NumericUpDown1.Value() & "', '" & TextBox10.Text & "','" & _
                      TextBox11.Text & "', '" & TextBox12.Text & "', '" & TextBox13.Text & "', '" & _
                      TextBox14.Text & "', '" & TextBox15.Text & "', '" & TextBox16.Text & "', '" & _
                      TextBox17.Text & "', '" & TextBox18.Text & "', '" & TextBox19.Text & "', '" & _
                      TextBox20.Text & "', '" & TextBox21.Text & "', '" & TextBox22.Text & "', '" & _
                      TextBox23.Text & "', '" & TextBox24.Text & "', '" & DateTimePicker2.Value() & "')"
           Try
               cnn.Open()
               adptr.InsertCommand = New OleDbCommand(sql, cnn)
               adptr.InsertCommand.ExecuteNonQuery()
               MsgBox("Data has been added!")
               adptr.Dispose()
               cnn.Close()
           Catch ex As Exception

               MsgBox(ex.ToString)
           End Try

推荐答案

ComboBox.SelectedItem [ ^ ]返回对象 - > ComboBox中当前选定的项目。

使用 SelectedText [ ^ ]或 SelectedIndex [ ^ ] property使用 .ToString()方法。



您可以在这里找到一些有用的信息:

ADO.NET [ ^ ]

http://msdn.microso ft.com/en-us/library/ms971485.aspx [ ^ ]

使用ADO.NET Access数据库 [ ^ ]

为初学者使用ADO.NET [ ^ ]
ComboBox.SelectedItem[^] returns object -> currently selected item in the ComboBox.
Use SelectedText[^] or SelectedIndex[^] property with .ToString() method.

Some useful information you''ll find here:
ADO.NET[^]
http://msdn.microsoft.com/en-us/library/ms971485.aspx[^]
Using ADO.NET Access database[^]
Using ADO.NET for beginners[^]


这是一个HORRIFYING代码。



谷歌的SQL注入攻击为什么我这么说以及你能做些什么呢。然后谷歌为SQL参数化查询VB.NET在你的代码中做些什么。



Serisouly。这样做,您的代码将变得更容易调试,修复和支持。
That is HORRIFYING code.

Google for "SQL Injection Attack" for why I said that and what you can do about it. Then Google for "SQL Parameterized queries VB.NET" for what to do about it in your code.

Serisouly. Do that and your code will become FAR easier to debug, fix and support.


这篇关于如何将数据从VB添加和更新到MSAccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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