Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何从文本框更新我的Access数据库_Vb.net_Ms Access - Fatal编程技术网

Vb.net 如何从文本框更新我的Access数据库

Vb.net 如何从文本框更新我的Access数据库,vb.net,ms-access,Vb.net,Ms Access,我创建了一个屏幕,可以在其中编辑数据库中的详细信息。但只要我点击更新按钮,它就会显示一个或多个必需参数没有给定值。我已附上我的代码 更新按钮 Private Sub SimpleButton5_Click(sender As Object, e As EventArgs) Handles SimpleButton5.Click Try Access.AddParam("@UId", TextBox1.Text) Access.AddParam("@Im

我创建了一个屏幕,可以在其中编辑数据库中的详细信息。但只要我点击更新按钮,它就会显示一个或多个必需参数没有给定值。我已附上我的代码

更新按钮

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
My Access.ExecQuery---(类…)

我已经玩了两天了,但是在某个地方我错过了什么或者忽略了什么

thx


Jaco

错误信息非常明显。某些参数丢失,可能是您忘记了,也可能是拼写错误

你需要仔细检查你的代码,它包含了相当多的打字错误

  • 您正在定义参数
    @ImagePic
    ,但在查询中未使用该参数
  • @Barcod
    相同,您将其放在SQL:
    BarCode=Textbox2.text
    中。就叫它
    @Barcode
    ,你为什么这样缩写名字。这只会造成混乱。使用正确的英语拼写并保持一致
  • 另一个输入错误:
    Access.AddParam(“@BeforTax”,TextBox15.Text)
    。在SQL中:
    ProfirBefore=@BeforeTax
    profirbfore
    也是一个输入错误
  • 请帮自己一个忙,并重新命名文本框:TextBox1到20是不好的命名实践。在复制粘贴语句后,很有可能会混淆字段。Textbox20一点也不直观,不会告诉您正在处理哪些数据
我已经玩了两天了,但是在某个地方我错过了什么或者忽略了什么


缺少眼镜:)我不知道你的开发环境,我把你的代码粘贴在记事本++上,点击一个关键字,它会突出显示代码中出现的所有关键字。很快就很明显,有些关键字没有在任何地方被引用。

我为您的库存对象创建了一个简单的类,这样我就可以避免将所有属性作为参数传递。我可以将Inventory对象传递给
UpdateDatabase
方法

Public Class Inventory
    Public Property Picture As Byte()
    Public Property BarCode As String
    Public Property BrandName As String
    Public Property StockName As String
    Public Property Category As String
    Public Property SubCategory As String
    Public Property SubCategory2 As String
    Public Property Description As String
    Public Property StockLevels As Integer
    Public Property CustomAmount As Decimal
    Public Property CostPrice As Decimal
    Public Property MarkupAmount As Decimal
    Public Property SellingPrice As Decimal
    Public Property ProfitBefore As Decimal
    Public Property ProfitAfter As Decimal
    Public Property TaxAmount As Decimal
    Public Property taxPer As Decimal
    Public Property MarkupPer As Decimal
    Public Property LastDateupdated As Date
    Public Property UpserUpdated As String
    Public Property ID As Integer
End Class
为了使图片框图像具有适当的存储格式,我有一个小函数,它将
图像
作为参数,并返回一个字节数组

'This Function requires Imports System.Drawing
Private Function GetByteArrayFromImage(img As Image) As Byte()
    Dim convert As New ImageConverter
    Dim arr = DirectCast(convert.ConvertTo(img, GetType(Byte())), Byte())
    Return arr
End Function
库存对象的每个属性都已设置。您可以看到,在这里为控件指定有意义的名称会有所帮助<代码>CInt、
CDec
CDate
文本框需要在到达此代码之前进行验证

我将Try…Catch放在这里,以便您可以向用户显示消息

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim img As Image = PictureBox1.Image
    Dim imgArray = GetByteArrayFromImage(img)
    Dim inv As New Inventory With
    {
        .Picture = imgArray,
        .BarCode = TextBox2.Text,
        .BrandName = TextBox3.Text,
        .StockName = TextBox4.Text,
        .Category = TextBox5.Text,
        .SubCategory = TextBox6.Text,
        .SubCategory2 = TextBox7.Text,
        .Description = TextBox8.Text,
        .StockLevels = CInt(TextBox9.Text),
        .CustomAmount = CDec(TextBox10.Text),
        .CostPrice = CDec(TextBox11.Text),
        .MarkupAmount = CDec(TextBox12.Text),
        .SellingPrice = CDec(TextBox14.Text),
        .ProfitBefore = CDec(TextBox15.Text),
        .ProfitAfter = CDec(TextBox16.Text),
        .TaxAmount = CDec(TextBox13.Text),
        .taxPer = CDec(TextBox17.Text),
        .MarkupPer = CDec(TextBox18.Text),
        .LastDateupdated = CDate(TextBox19.Text),
        .UpserUpdated = TextBox20.Text,
        .ID = CInt(TextBox1.Text)
    }
    Try
        UpdateDatabase(inv)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
在Access中,参数在sql语句中的显示顺序必须与它们添加到参数集合的顺序相匹配

我觉得奇怪的是,除了taxPer之外,你所有的字段名都是大写的。检查你的数据库

使用…结束使用
块确保即使出现错误也关闭并释放数据库对象

.Add
方法优于您使用的方法,因为它包括数据类型和大小。我不得不猜测类型和尺寸,所以检查你的数据库。如果我猜错了,您必须更正
.Add
方法、
Inventory
类中属性的类型以及文本框的转换。text属性。(3个地方需要更换)


@ImagePic怎么样?部分问题在于图像。请参阅此链接,此链接指向Sql Server,但OleDb可以替代。感谢您的推荐。我是新来的。我尽我最大的努力去做一些我有点头绪的事情。非常感谢。对于刚开始学习的人来说,这就像金子一样。@Jaco989我很高兴这是有用的。您可以使用答案左侧数字上方的向上三角形向上投票。如果您的问题得到回答,您可以通过单击问题左侧的复选标记(勾选标记)接受答案。接受一个答案不仅会帮助未来的读者,还会为你赢得几次机会。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim img As Image = PictureBox1.Image
    Dim imgArray = GetByteArrayFromImage(img)
    Dim inv As New Inventory With
    {
        .Picture = imgArray,
        .BarCode = TextBox2.Text,
        .BrandName = TextBox3.Text,
        .StockName = TextBox4.Text,
        .Category = TextBox5.Text,
        .SubCategory = TextBox6.Text,
        .SubCategory2 = TextBox7.Text,
        .Description = TextBox8.Text,
        .StockLevels = CInt(TextBox9.Text),
        .CustomAmount = CDec(TextBox10.Text),
        .CostPrice = CDec(TextBox11.Text),
        .MarkupAmount = CDec(TextBox12.Text),
        .SellingPrice = CDec(TextBox14.Text),
        .ProfitBefore = CDec(TextBox15.Text),
        .ProfitAfter = CDec(TextBox16.Text),
        .TaxAmount = CDec(TextBox13.Text),
        .taxPer = CDec(TextBox17.Text),
        .MarkupPer = CDec(TextBox18.Text),
        .LastDateupdated = CDate(TextBox19.Text),
        .UpserUpdated = TextBox20.Text,
        .ID = CInt(TextBox1.Text)
    }
    Try
        UpdateDatabase(inv)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
Private Sub UpdateDatabase(inv As Inventory)
    Dim sql = "UPDATE Inventory SET  
                [Image]=@Picture, 
                BarCode= @BarCode,
                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"
    Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb;"),
            cmd As New OleDbCommand(sql, cn)
        With cmd.Parameters
            .Add("@Picture", OleDbType.LongVarBinary).Value = inv.Picture
            .Add("@BarCode", OleDbType.VarChar, 100).Value = inv.BarCode
            .Add("@BrandName", OleDbType.VarChar, 100).Value = inv.BrandName
            .Add("@StockName", OleDbType.VarChar, 100).Value = inv.StockName
            .Add("@Category", OleDbType.VarChar, 100).Value = inv.Category
            .Add("@SubCat", OleDbType.VarChar, 100).Value = inv.SubCategory
            .Add("@SubCat2", OleDbType.VarChar, 100).Value = inv.SubCategory2
            .Add("@Discrip", OleDbType.VarChar, 100).Value = inv.Description
            .Add("@StockLvl", OleDbType.Integer).Value = inv.StockLevels
            .Add("@Customamount", OleDbType.Decimal).Value = inv.CustomAmount
            .Add("@CostPrice", OleDbType.Decimal).Value = inv.CostPrice
            .Add("@Markup", OleDbType.Decimal).Value = inv.MarkupAmount
            .Add("@SellingPrice", OleDbType.Decimal).Value = inv.SellingPrice
            .Add("@BeforeTax", OleDbType.Decimal).Value = inv.ProfitBefore
            .Add("@AfterTax", OleDbType.Decimal).Value = inv.ProfitAfter
            .Add("@TaxAmount", OleDbType.Decimal).Value = inv.TaxAmount
            .Add("@TaxPer", OleDbType.Decimal).Value = inv.taxPer
            .Add("@MarkupPer", OleDbType.Decimal).Value = inv.MarkupPer
            .Add("@LAstDate", OleDbType.Date).Value = inv.LastDateupdated
            .Add("@LastUser ", OleDbType.VarChar, 100).Value = inv.UpserUpdated
            .Add("@UId", OleDbType.Integer).Value = inv.ID
        End With
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub