如何使用vb.net实现减量化
出现错误(mscorlib.dll中发生类型为“System.ArgumentOutOfRangeException”的未处理异常 附加信息:索引和长度必须引用字符串中的位置。)如何使用vb.net实现减量化,vb.net,Vb.net,出现错误(mscorlib.dll中发生类型为“System.ArgumentOutOfRangeException”的未处理异常 附加信息:索引和长度必须引用字符串中的位置。) 我想你跟不上这句话 mycmd.CommandText = "Update inventory set total_quantity=total_quantity-'" & dami & "' where item_code='" & itemcodet.Text & "'
我想你跟不上这句话
mycmd.CommandText = "Update inventory set total_quantity=total_quantity-'" & dami & "' where item_code='" & itemcodet.Text & "'"
应该是
mycmd.CommandText = "Update inventory set total_quantity=total_quantity-" & dami & " where item_code='" & itemcodet.Text & "'"
使用整数计数时不要使用撇号。。撇号仅用于字符串的情况我使用SQL server提供程序进行演示。更改到您正在使用的任何数据库。检查数据库中字段的实际数据类型。在最后一分钟打开连接。请参阅我关于使用块的评论
Private Sub OPCode()
Using myconnection As New SqlConnection("Your connection string")
Using mycmd As New SqlCommand("Update inventory set total_quantity = total_quantity - @dami where item_code = @itemCode;", myconnection)
mycmd.Parameters.Add("@dami", SqlDbType.Int).Value = CInt(quantityt.Text)
mycmd.Parameters.Add("@itemCode", SqlDbType.VarChar).Value = itemcodet.Text
myconnection.Open()
mycmd.ExecuteNonQuery()
End Using
End Using
MsgBox("stocks decrease!!", MsgBoxStyle.Information, "Noticed..")
End Sub
不要连接字符串以生成Sql查询。使用参数。您的代码存在SQL注入的风险,SQL注入可能会破坏数据库。连接对象不仅需要关闭,还需要释放。即使出现错误,Using…End Using块也会负责关闭和处理。如果在连接关闭之前放置MessageBox,用户可能已经去吃午饭了,而连接在微风中飘荡,等待单击“确定”按钮。代码中的哪一行导致了错误?这解决了当前问题,但这是个糟糕的建议。教用户使用参数。
Private Sub OPCode()
Using myconnection As New SqlConnection("Your connection string")
Using mycmd As New SqlCommand("Update inventory set total_quantity = total_quantity - @dami where item_code = @itemCode;", myconnection)
mycmd.Parameters.Add("@dami", SqlDbType.Int).Value = CInt(quantityt.Text)
mycmd.Parameters.Add("@itemCode", SqlDbType.VarChar).Value = itemcodet.Text
myconnection.Open()
mycmd.ExecuteNonQuery()
End Using
End Using
MsgBox("stocks decrease!!", MsgBoxStyle.Information, "Noticed..")
End Sub