【发布时间】:2021-09-12 18:48:01
【问题描述】:
我有一个VB.NET表格的图表,标题中有错误:DBDA.Fill(DS, "chtRevenue")
我有什么遗漏或者此代码不正确吗?
我是编码新手,以前从未使用过图表。
图表应该从访问数据库中提取日期。
这是与问题相关的所有代码:
Imports System.Data.OleDb
Imports System.Windows.Forms.DataVisualization.Charting
Public Class frmSalesAndRevenue
Private DB As New DBControl
Dim DBDA As New OleDbDataAdapter
Dim DS As New DataSet
Private DBCmd As New OleDbCommand
Private READ As OleDbDataReader
Private DBCon As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" &
"Data Source=|DataDirectory|\NewHotel.mdb;")
Private Function NotEmpty(text As String) As Boolean
Return Not String.IsNullOrEmpty(text)
End Function
Private Sub frmSalesAndRevenue_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DBCon.Open()
DBCmd = New OleDbCommand
DBDA = New OleDbDataAdapter(DBCmd)
DBDA.Fill(DS, "chtRevenue")
DBCon.Close()
End Sub
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
'Clear old graph and plot new graph
chtRevenue.ChartAreas.Clear()
chtRevenue.ChartAreas.Add("ChartArea1")
With chtRevenue.ChartAreas("ChartArea1")
.AxisX.Title = "DateBooked"
.AxisX.MajorGrid.LineColor = Color.Purple
.AxisY.Title = "Revenue"
.AxisX.MajorGrid.LineColor = Color.Purple
End With
Dim Series As Series = chtRevenue.Series("revenue received")
chtRevenue.DataSource = DS.Tables("chtRevenue")
'Clear series and add new series
chtRevenue.Series.Clear()
chtRevenue.Series.Add("revenue received")
chtRevenue.Series("revenue received").Color = Color.Purple
chtRevenue.Series("revenue received").ChartType = DataVisualization.Charting.SeriesChartType.Column
With chtRevenue
.Series(0).XValueMember = "DateBooked"
.Series(0).XValueMember = "Revenue"
End With
Dim x As DateTime
Dim y As Int32
chtRevenue.Series("revenue received").Points.Add(x.ToOADate(), y)
End Sub
感谢您的宝贵时间:)
【问题讨论】:
-
@Steeeve 是的,它会在行上抛出错误
System.InvalidOperationException: 'Fill: SelectCommand.Connection property has not been initialized.':DBDA.Fill(DS, "chtRevenue") -
那是因为在您的
LoadChart方法中,您没有指定如何从数据库中读取数据。DBCmd没有指定 CommandText。还不清楚您的变量DS是什么。您应该发布更多代码。 -
@Steeeve 对不起,我忘了那部分
-
好吧,如果这就是所有的代码,你不能在不告诉DataAdapter如何从数据库中读取数据的情况下填充DataSet
DS。我建议看看ADO.NET code examples。一旦你的数据集填满了正确的数据,你就可以进入关于图表的第二步(如果你仍然有问题)。 -
@Steeeve 谢谢,我去看看