2010-04-27 70 views
0

這是我的問題:檢索表中一列的值

我有2個表帳戶和事務日誌。賬戶表中有 ,它有一個「金額」欄,它是賬戶的基本金額。 ,在Trans Logs表中,它還有一列「金額」,它是帳戶的額外(增加或減去基本金額)金額。 ,我不知道如何檢索該基本數量來編輯它,然後將其保存回表格。 這意味着我需要通過使用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* 

謝謝!

+0

'昏暗'似乎表明您正在使用某些版本的Basic? – 2010-04-27 17:39:54

+0

我正在使用VS 2008與VB – user327094 2010-04-27 18:23:12

回答

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