如何从文本框更新我的 Access 数据库
我创建了一个屏幕,我可以在其中编辑数据库中的详细信息.但是一旦我点击更新按钮,它就会说没有为一个或多个必需参数提供值.我附上了我的代码....
i created a screen where i can edit the details in the database. but as soon as i click on the update button, it says no value given for one or more required parameters. i have attached my code....
更新按钮...
Private Sub SimpleButton5_Click(sender As Object, e As EventArgs) Handles SimpleButton5.Click
Try
Access.AddParam("@UId", TextBox1.Text)
Access.AddParam("@ImagePic", PictureBox1.Image)
Access.AddParam("@Barcod", TextBox2.Text)
Access.AddParam("@BrandName", TextBox3.Text)
Access.AddParam("@StockName", TextBox4.Text)
Access.AddParam("@Category", TextBox5.Text)
Access.AddParam("@SubCat", TextBox6.Text)
Access.AddParam("@Subcat2", TextBox7.Text)
Access.AddParam("@Discrip", TextBox8.Text)
Access.AddParam("@StockLvl", TextBox9.Text)
Access.AddParam("@CustomAmount", TextBox10.Text)
Access.AddParam("@CostPrice", TextBox11.Text)
Access.AddParam("@Markup", TextBox12.Text)
Access.AddParam("@TaxAmount", TextBox13.Text)
Access.AddParam("@SellingPrice", TextBox14.Text)
Access.AddParam("@BeforTax", TextBox15.Text)
Access.AddParam("@AfterTax", TextBox16.Text)
Access.AddParam("@TaxPer", TextBox17.Text)
Access.AddParam("@MarkupPer", TextBox18.Text)
Access.AddParam("@LastDate", TextBox19.Text)
Access.AddParam("@LastUser", TextBox20.Text)
Access.ExecQuery("UPDATE Inventory " &
"SET [Image]=PictureBox1.image, BarCode=Textbox2.text, " &
"BrandName=@BrandName, StockName=@StockName, Category=@Category, SubCategory=@SubCat, " &
"SubCategory2=@SubCat2, Description=@Discrip, StockLevels=@StockLvl, CustomAmount=@Customamount, " &
"CostPrice=@CostPrice, MarkupAmount=@Markup, SellingPrice=@SellingPrice, ProfirBefore=@BeforeTax, " &
"ProfitAfter=@AfterTax, TaxAmount=@TaxAmount, taxPer=@TaxPer, MarkupPer=@MarkupPer, LastDateupdated=@LAstDate, " &
"UpserUpdated=@LastUser WHERE ID=@UId")
If NoErrors(True) = False Then Exit Sub
RefreshData()
Catch ex As Exception
MsgBox(ex.Message)
Finally
End Try
End Sub
我的 Access.ExecQuery ---(类...)
My Access.ExecQuery --- (Class...)
Imports System.Data.OleDb
Public Class DBControl
Private DBCon As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb;")
Private DBCmd As OleDbCommand
Public DBDA As OleDbDataAdapter
Public DBDT As DataTable
Public Params As New List(Of OleDbParameter)
Public RecordCount As Integer
Public Exception As String
Public Sub ExecQuery(Query As String)
RecordCount = 0
Exception = ""
Try
DBCon.Open()
DBCmd = New OleDbCommand(Query, DBCon)
Params.ForEach(Sub(p) DBCmd.Parameters.Add(p))
Params.Clear()
DBDT = New DataTable
DBDA = New OleDbDataAdapter(DBCmd)
RecordCount = DBDA.Fill(DBDT)
Catch ex As Exception
Exception = ex.Message
End Try
If DBCon.State = ConnectionState.Open Then DBCon.Close()
End Sub
' INCLUDE QUERY & COMMAND PARAMETERS
Public Sub AddParam(Name As String, Value As Object)
Dim NewParam As New OleDbParameter(Name, Value)
Params.Add(NewParam)
End Sub
End Class
我已经玩了 2 天了,但在某个地方我遗漏了一些东西或忽略了一些东西
I have played around with this for 2 days now, but somewhere i am missing something or overlooking something
谢谢
雅各
错误信息非常明显.缺少某些参数,要么是您忘记了它们,要么是拼写错误.
The error message is pretty obvious. Some parameters are missing, either you forgot them or they are misspelled.
你需要仔细检查你的代码,它包含很多错别字.
You need to double-check your code, it contains quite a few typos.
- 您正在定义参数
@ImagePic
,但它并未在您的查询中使用. - 对于
@Barcod
也是一样,你把它放在你的 SQL 中:BarCode=Textbox2.text
.就叫它@Barcode
,你为什么要这样缩写名字.那只会造成混乱.使用正确的英语拼写并保持一致. - 另一个错字:
Access.AddParam("@BeforTax", TextBox15.Text)
.在您的 SQL 中:ProfirBefore=@BeforeTax
.ProfirBefore
也是一个错字. - 请帮自己一个忙并重命名文本框:TextBox1 到 20 不是一个好的命名习惯.在复制粘贴您的语句后,您很有可能会混淆字段.Textbox20 一点也不直观,也不会告诉您正在处理哪些数据.
- You are defining parameter
@ImagePic
, but it's not used in your query. - Same for
@Barcod
, you put this instead in your SQL:BarCode=Textbox2.text
. Just call it@Barcode
, why do you abbreviate names like that. That only creates confusion. Use proper English spelling and be consistent. - Another typo:
Access.AddParam("@BeforTax", TextBox15.Text)
. In your SQL:ProfirBefore=@BeforeTax
.ProfirBefore
is a typo too. - Please do yourself a favor and rename the textboxes too: TextBox1 thru 20 is not good naming practice. There is good chance that you will mix up fields after doing copy-paste of your statements. Textbox20 is not intuitive at all and does not tell you what data you are handling.
我已经玩了 2 天了,但在某个地方我遗漏了一些东西或忽略了一些东西
I have played around with this for 2 days now, but somewhere i am missing something or overlooking something
可能是缺少眼镜 :) 我不知道您的开发环境如何,我将您的代码粘贴到 Notepad++ 中,通过单击关键字,它会突出显示代码中所有出现的该关键字.很快就发现有些关键字没有在任何地方被引用.
Missing glasses perhaps :) I don't know about your development environment put I pasted your code in Notepad++ and by clicking on a keyword it highlights all occurrences of that keyword in the code. It quickly became obvious that some keywords were not being referenced anywhere.