我有一个名为CarsType.accdb的数据库,数据库Item_Name,Item_Num,Item_Qty,Item_Cost中有四个字段。
我能够让数据库在VisualBasic中显示我的数据,但我不知道如何在我的标签中显示总成本(lblTotalCost)。我更喜欢用VB而不是写入我的访问程序。所有我想做的事情都会成倍增加item_qty * Item_Cost
我将如何做到这一点?
Public Class frmCarInventory
Private Sub CarInventoryBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CarInventoryBindingNavigatorSaveItem.Click
Me.Validate()
Me.CarInventoryBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CarDataSet)
End Sub
Private Sub frmCarInventory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'CarDataSet.CarInventory' table. You can move, or remove it, as needed.
Me.CarInventoryTableAdapter.Fill(Me.CarDataSet.CarInventory)
Try
Me.CarInventoryTableAdapter.Fill(Me.CarDataSet.CarInventory)
Catch ex As Exception
MsgBox("The Database Files is Unavailable", , "Error")
End Try
End Sub
Private Sub btnComputeTheTotalValueOfInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeTheTotalValueOfInventory.Click
Dim strSql As String = "SELECT * FROM CarType "
'strPath provides the database type and path of the CarType database.
Dim strPath As String = "Provider=Microsoft.ACE.OLEDB.12.0 ;" & "Data Source=..\CarType.accdb"
Dim odaInventory As New OleDb.OleDbDataAdapter(strSql, strPath)
Dim DatCost As New DataTable
Dim intCount As Integer
Dim decTotalCost As Decimal = 0D
'The DataTable name datCost is filled with the data
odaInventory.Fill(DatCost)
'The connection to the databsise is disconnected
odaInventory.Dispose()
For intCount = 0 To DatCost.Rows.Count - 1
decTotalCost += Convert.ToDecimal(DatCost.Rows(intCount)("Total Inventory Cost"))
Next
Me.lblTotalCost.Visible = True
Me.lblTotalCost.Text = "El Value " & decTotalCost.ToString("C")
End Sub
End Class
这会像sql satement一样处理吗?
答案 0 :(得分:1)
将新System.Data.DataColumn
添加到System.Data.DataTable
时,可以指定表达式,从而创建计算列。在C#中,执行此操作的语法是:
dataTable.Columns.Add("total_cost", typeof(double), "item_qty * item_cost");
在VB.NET中执行此操作的语法非常相似,但我将其留作读者练习。
答案 1 :(得分:0)
也许你可以使用这样的东西?
Dim TotalCost As String
TotalCost = Me![item_qty] * Me![item_cost]
Me![lblTotalCost].Caption = TotalCost