更新语句未更新

时间:2013-11-27 01:05:37

标签: vb.net

帮助!..当我添加一个例子555555的发票..然后当我更新到565656它没有更新..我的代码中的错误是什么?

If Me.txtInvoice.Tag = 0 Then
     sSQL = "INSERT INTO CollectionTbl (invoice, client, amount, invoice_dated, _
       service, sr_dated, sr, amount_paid, date_paid, period, balance, job, _
       address, tin, render, or_no)"

     sSQL = sSQL & "  VALUES(@invoice, @client, @amount, @invoice_dated, _
       @service, @sr_dated, @sr, @amount_paid, @date_paid, @period, @balance, _
       @job, @address, @tin, @render, @or_no)"

     cmd.CommandText = sSQL
Else
     sSQL = "UPDATE CollectionTbl set  invoice = @invoice, client =  _
       @client, amount =  @amount, invoice_dated = @invoice_dated"
     sSQL = sSQL & " , service = @service, sr_dated = @sr_dated, _
        sr = @sr, amount_paid = @amount_paid, date_paid = @date_paid, _
        period = @period, balance = @balance, job = @job, address = @address, _
        tin = @tin, render = @render, or_no=@or_no where invoice = @invoice"
     cmd.CommandText = sSQL
 End If

  cmd.Parameters.Add("@invoice", OleDbType.Numeric).Value = IIf(Len(Trim(Me.txtInvoice.Text)) > 0, Me.txtInvoice.Text, DBNull.Value)
  cmd.Parameters.Add("@client", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtClient.Text)) > 0, Me.txtClient.Text, DBNull.Value)
  cmd.Parameters.Add("@amount", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtAmount.Text)) > 0, Me.txtAmount.Text, DBNull.Value)
  cmd.Parameters.Add("@invoice_dated", OleDbType.Date).Value = IIf(Len(Trim(Me.dtpCD.Text)) > 0, Me.dtpCD.Text, DBNull.Value)
  cmd.Parameters.Add("@service", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtService.Text)) > 0, Me.txtService.Text, DBNull.Value)
  cmd.Parameters.Add("@sr_dated", OleDbType.Date).Value = IIf(Len(Trim(Me.dtpSR.Text)) > 0, Me.dtpSR.Text, DBNull.Value)
  cmd.Parameters.Add("@sr", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtSr.Text)) > 0, Me.txtSr.Text, DBNull.Value)
  cmd.Parameters.Add("@amount_paid", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtPaid.Text)) > 0, Me.txtPaid.Text, DBNull.Value)
  cmd.Parameters.Add("@date_paid", OleDbType.Date).Value = IIf(Len(Trim(Me.dtpDP.Text)) > 0, Me.dtpDP.Text, DBNull.Value)
  cmd.Parameters.Add("@period", OleDbType.Date).Value = IIf(Len(Trim(Me.dtpPD.Text)) > 0, Me.dtpPD.Text, DBNull.Value)
  cmd.Parameters.Add("@balance", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtBal.Text)) > 0, Me.txtBal.Text, DBNull.Value)
  cmd.Parameters.Add("@job", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtJD.Text)) > 0, Me.txtJD.Text, DBNull.Value)
  cmd.Parameters.Add("@address", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtRich.Text)) > 0, Me.txtRich.Text, DBNull.Value)
  cmd.Parameters.Add("@tin", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtTin.Text)) > 0, Me.txtTin.Text, DBNull.Value)
  cmd.Parameters.Add("@render", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtRender.Text)) > 0, Me.txtRender.Text, DBNull.Value)
  cmd.Parameters.Add("@or_no", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtOR.Text)) > 0, Me.txtOR.Text, DBNull.Value)

  cmd.Parameters.Add("@invoice", OleDbType.Numeric).Value = Me.txtInvoice.Text
  cmd.ExecuteNonQuery()

  'If the record is new then we have to get its ID so that 
  'we can edit it rightaway after the insertion.
If Me.txtInvoice.Tag = 0 Then
    cmd.CommandText = "Select @@Identity"
           'Set textbox tag property with the ID of new record
    Me.txtInvoice.Tag = cmd.ExecuteScalar()
End If
MsgBox("Data has been save.")

1 个答案:

答案 0 :(得分:0)

它不会更新,因为如果您将55555的发票更新为55565(或任何其他),查询中会发生什么

UPDATE tablename SET invoice = 55565 WHERE invoice = 55565 - >我假设还没有55565的发票,所以不会发生更新。

解决方案是获取您要首先更新的发票的值(在我的示例中为55555,@ innoice)并为您想要更新的那个存储不同的值(在示例中为55565, @ invoice2)

修改

在您的示例中,您说您添加了555555的发票。因此,您的表CollectionTbl现在的发票为555555

CollectionTbl
invoice | client | etc... |
____________________________
555555  | exmple | etc... |

现在,您要将发票555555更新为565656吗?
你做了什么是

UPDATE CollectionTbl set  invoice = @invoice etc... WHERE invoice = @invoice

此处@invoice等于565656

回顾CollectionTbl,您看不到565656的发票,只有555555.因此您无法更新它。您所要做的就是获取您在CollectionTbl上添加的发票,即555555,然后在WHERE子句中使用它。