检索表中一列的值

时间:2010-04-27 17:25:36

标签: vb.net

这是我的问题:

我有2个表帐户和交易日志。 在“帐户”表中,它具有“金额”列,这是帐户的基本金额。 在Trans Logs表中,它还有“Amount”列,它是帐户的额外(基本金额的增加或减少)金额。 我不知道如何检索该基本数量进行编辑,然后将其保存回表格。 这意味着我需要通过使用Acc_No来查找正确列的值。顺便说一句,我正在使用DataSet。 我认为它应该是这样的:

Dim Amount as Decimal
Amount = *the code to retrieve the base amount*
Amount = Amount + txtAmount.Text
*the code to save the new amount back to Accounts table*

谢谢!

1 个答案:

答案 0 :(得分:0)

Private Sub UpdateAmount(ByVal Acc_No As Integer, ByVal Amt As Double, ByVal Acc_Type As String)

    'Retrieve columns from accounts table
    Dim cnntStr As String = "Data Source=DUC-91D85F3F8C2\SQLEXPRESS;Initial Catalog=BasicAccounting;Integrated Security=True"
    Dim cn As New SqlConnection(cnntStr)
    cn.Open()
    Dim da As SqlDataAdapter
    Dim ds As New Data.DataSet
    Dim stmt As String = "SELECT Amount, Acc_Type FROM ACCOUNTS WHERE Acc_No = " & Acc_No
    da = New SqlDataAdapter(stmt, cn)
    da.Fill(ds, "ACCOUNTS")

    'To modify the amount
    Dim type As String
    type = ds.Tables(0).Rows(0).Item("Acc_Type")
    Dim amount As Decimal
    amount = ds.Tables(0).Rows(0).Item("Amount")

    If type = Acc_Type Then
        amount = amount + Amt
    ElseIf type <> Acc_Type Then
        amount = amount - Amt
    End If

    'To update the amount
    stmt = "UPDATE ACCOUNTS SET Amount = " & amount & " WHERE Acc_No = " & Acc_No
    da = New SqlDataAdapter(stmt, cn)
    da.Fill(ds, "ACCOUNTS")
    da.Update(ds, "ACCOUNTS")
End Sub