[求助]更新声明VB.net的问题 [英] [SOLVED] Problem with Update Statement VB.net

查看:77
本文介绍了[求助]更新声明VB.net的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



请将我的更新声明存在于当前为小型车店编码的程序中。它是一个汽车管理系统,作为一个新手,我被卡住了。

在我的OLE数据库中,我有几张桌子,但与我的问题有关的是:

tlbCar - 此表包含所有在相应库存中注册的汽车。

tlbSales - 这包含公司的销售记录。

我打算以这种特殊形式做的是当客户来买车,操作员选择任何车型(我有一个文本框,由tlbCar表中的相应字段自动填充,显示该车的当前库存)并单击保存按钮。我想要发生两件事。一个实际保存当前事务的记录和更新tlbCar(通过从数据库中的金额减去客户想要购买的金额)与减法的结果。得到的错误是在发出订单时,我注意到没有从数据库中扣除。

谢谢



< pre lang =vb> 私有 Sub Create_Click( ByVal sender As System。 Object ByVal e As System.EventArgs)句柄 Create.Click
UpdateStock()
AddRec()
结束 Sub
公共 Sub AddRec()
Dim cmdtext 作为 字符串 = 插入tlbSales& _
(Ord_ID,Ord_Date,Cus_Name,Model,Qty,Price,Total,Mode)& _
值(@ OrdID,@ OrdDate,@ CusName,@ CarModel,@ OrdQty,@ OrdPrice, @ OrdTotal,@ PayMode)
Dim cmd As OleDbCommand(cmdtext,con)
使用 cmd.Parameters
.Add( OleDbParameter( @ OrdID,OrdID.Text) )
.Add( New OleDbParameter( @OrdDate CDate (OrdDate.Text)))
.Add( New OleDbParameter( @ CusName,CusName.Text))
.Add(< span class =code-keyword >新 OleDbParameter( @ CarModel,CarModel.Text))
.Add( New OleDbParameter( @ OrdQty,OrdQty.Text))
.Add( New OleDbParameter( @ OrdPrice,CarPrice.Text))
.Add( New OleDbParameter( @ OrdTotal,OrdTotal.Text))
.Add( OleDbParameter( @ PayMode,PayMode.Text))
< span class =code-keyword>结束 使用
如果 con。 State = ConnectionState.Open The n
con.Close()
结束 如果
con.Open()
尝试
cmd.ExecuteNonQuery()
SalesDetails()
Clear.PerformClick( )
MsgBox( 订单交易已完成& & OrdID.Text,MsgBoxStyle.Information, 成功操作
Catch ex As Exception
MsgBox(ex.Message)
结束 尝试
con.Close()
结束 < span class =code-keyword> Sub

公开 Sub UpdateStock()
如果 con.State = ConnectionState.Open 那么
con.Close()
结束 如果
con.Open()
Dim cmdtext 作为 字符串 = < span class =code-string> UPDATE tlbCar SET Car_Quantity = @CQty,Date_Modified = @DMod WHERE Car_ID = @CID
Dim cmd 作为 OleDbCommand(cmdtext,con)
使用 cmd.Parameters
.Add( OleDbParameter( @ CID,CarID.Text))
.Add( New OleDbParameter( @ CQty,Stock.Text))
.Add( OleDbParameter( @ DMod CDate (OrdDate.Text)))
结束 使用
尝试
cmd.ExecuteNonQuery()
MsgBox( Stock Updated & & OrdID.Text,MsgBoxStyle.Information, 成功操作
Catch ex As 异常
结束 尝试
con.Close()
结束 Sub

解决方案

Jesse,



你没有显示您正在更新Stock.Text的位置。如果这是库存的原始数量,则使用其现有值更新数据库。我想你想通过OrdQty.Text减少Stock.Text或者SET Car_Quantity = Car_Quantity - @OrdQty。



我可以在这里看到其他一些潜在的问题:

1)如果插入成功但更新没有,会发生什么?我自己,我建议将两个数据库写操作放入存储过程并通过事务将它们包围起来。

2)并发:如果两个单独的连接尝试更新库存数量或者更新它会发生什么来自客户端的过时状态。出于这个原因,我总是使用更加动态的更新来减少库存量。将此放在事务中(可能是客户端而不是使用存储过程)会使操作原子化并消除任何并发问题。如果您需要应用程序中的当前库存量,您可以从存储过程中返回库存量。

3)此处的另一个并发问题是另一个进程可能已售出所有库存,因此随后的竞争过程会使股票变成负数。



也许您已经考虑过这些问题并且正在处理它们的单一连接或其他方式。我敦促你考虑这些并加入适当的保护措施!


这个片段解决了它,我知道它只是因为我不知道为什么参数让我们追逐鬼魂。

 公共  Sub  UpdateStock()
如果 con.State = ConnectionState.Open 那么
con.Close()
结束 如果
con.Open()
Dim cmdtext 作为 字符串 = UPDATE tlbCar SET Car_Quantity =& Stock.Text& & _
,Date_Modified ='& CDate (OrdDate.Text)& '& WHERE Car_ID =& CarID.Text&
Dim cmd 作为 OleDbCommand(cmdtext,con)
尝试
cmd.ExecuteNonQuery()
MsgBox( Stock Updated& & OrdID.Text,MsgBoxStyle.Information, 成功操作
Catch ex As 异常
MsgBox(ex.Message)
结束 尝试
con.Close()
结束 Sub



谢谢你时间


Hi All

Please i'm having problems with my update statement in a program in currently coding for small car shop. Its a car management system and as a rookie I'm stuck.
In my OLE database i have a couple of tables but the ones concerned with my problem is:
tlbCar - This table holds all the cars registered with their corresponding stock.
tlbSales - This holds sales record in the company.
What i intend doing in this particular form is that when a customer comes to buy a car and the operator selects the model of any car (i've got a textbox which becomes automatically populated by the corresponding field in the tlbCar table showing the current stock of that car) and clicks the save button. I want two things to happen. One to actually save the record for the current transaction and to Update tlbCar (by subtracting amount the customer wants to buy from the amount in the database) with the result of the subtraction. The error is get is that on firing the order form i notice that there wasn't and deduction from the database.
Thanks

Private Sub Create_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Create.Click
        UpdateStock()
        AddRec()
    End Sub
Public Sub AddRec()
        Dim cmdtext As String = "Insert Into tlbSales" & _
                                "(Ord_ID, Ord_Date, Cus_Name, Model, Qty, Price, Total, Mode)" & _
                                "Values (@OrdID,@OrdDate,@CusName,@CarModel,@OrdQty,@OrdPrice,@OrdTotal,@PayMode)"
        Dim cmd As New OleDbCommand(cmdtext, con)
        With cmd.Parameters
            .Add(New OleDbParameter("@OrdID", OrdID.Text))
            .Add(New OleDbParameter("@OrdDate", CDate(OrdDate.Text)))
            .Add(New OleDbParameter("@CusName", CusName.Text))
            .Add(New OleDbParameter("@CarModel", CarModel.Text))
            .Add(New OleDbParameter("@OrdQty", OrdQty.Text))
            .Add(New OleDbParameter("@OrdPrice", CarPrice.Text))
            .Add(New OleDbParameter("@OrdTotal", OrdTotal.Text))
            .Add(New OleDbParameter("@PayMode", PayMode.Text))
        End With
        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        Try
            cmd.ExecuteNonQuery()
            SalesDetails()
            Clear.PerformClick()
            MsgBox("Order Transaction Completed" & " " & OrdID.Text, MsgBoxStyle.Information, "Sucessful Operation")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        con.Close()
    End Sub

Public Sub UpdateStock()
        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        Dim cmdtext As String = "UPDATE tlbCar SET Car_Quantity = @CQty, Date_Modified = @DMod WHERE Car_ID = @CID"
        Dim cmd As New OleDbCommand(cmdtext, con)
        With cmd.Parameters
            .Add(New OleDbParameter("@CID", CarID.Text))
            .Add(New OleDbParameter("@CQty", Stock.Text))
            .Add(New OleDbParameter("@DMod", CDate(OrdDate.Text)))
        End With
        Try
            cmd.ExecuteNonQuery()
            MsgBox("Stock Updated" & " " & OrdID.Text, MsgBoxStyle.Information, "Sucessful Operation")
        Catch ex As Exception
        End Try
        con.Close()
    End Sub

解决方案

Jesse,

You don't show where you are updating Stock.Text. If this is the original amount of the stock, you are updating the database with its existing value. I think that you want to reduce Stock.Text by OrdQty.Text or "SET Car_Quantity = Car_Quantity - @OrdQty".

I can see a few other potential issues here:
1) what happens if the insert succeeds but the update doesn't? Myself, I'd suggest putting both database write operations into a stored procedure and surrounding them by a transaction.
2) Concurrency: What happens if two separate connections attempt to update the stock quantity or it is updated from out-of-date state on the client side. For this reason, I'd always use the more dynamic update of reducing the stock amount. Placing this in a transaction (which could be client side instead using a stored proc) makes the operation "atomic" and removes any concurrency issue. If you need the current amount of stock in the application, you could return the amount of stock from the stored proc.
3)Another concurrency issue here is that another process might have sold all stock, so a subsequent competing process puts stock into a negative amount.

Maybe you had already considered these issues and are dealing with them a single connection or some other way. I'd urge you to think about these and incorporate the appropriate safeguards!


This snippet solves it which i know it would just that i don't know why with parameters its us chasing ghosts.

Public Sub UpdateStock()
        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        Dim cmdtext As String = "UPDATE tlbCar SET Car_Quantity =" & Stock.Text & "" & _
                                ", Date_Modified = '" & CDate(OrdDate.Text) & "'" & " WHERE Car_ID = " & CarID.Text & ""
        Dim cmd As New OleDbCommand(cmdtext, con)
        Try
            cmd.ExecuteNonQuery()
            MsgBox("Stock Updated" & " " & OrdID.Text, MsgBoxStyle.Information, "Sucessful Operation")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        con.Close()
    End Sub


Thanks for ya time


这篇关于[求助]更新声明VB.net的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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