System.InvalidOperationException:“填充:SelectCommand.Connection 属性尚未初始化。”

Posted

技术标签:

【中文标题】System.InvalidOperationException:“填充:SelectCommand.Connection 属性尚未初始化。”【英文标题】:System.InvalidOperationException: 'Fill: SelectCommand.Connection property has not been initialized.' 【发布时间】: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 谢谢,我去看看 【参考方案1】:
Private Function NotEmpty(text As String) As Boolean
    Return Not String.IsNullOrEmpty(text)
End Function

这个函数似乎没有太大的用途。只需在您的代码中直接使用String.IsNullOrEmpty() 方法即可。

不要在使用它们的方法之外声明连接。 Connections 和 DataAdapters 需要处理。为此目的提供了Using...End Using 块。 DataAdapter 的构造函数可以获取CommandText 和连接。 DataAdapter 将作为Fill 方法的一部分打开和关闭连接。

Dim DS As New DataSet
Private MyConStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\NewHotel.mdb;"

Private Sub frmSalesAndRevenue_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Using DBCon As New OleDbConnection(MyConStr),
            DBDA As New OleDbDataAdapter("Select SomeFields From SomeTable", DBCon)
        DBDA.Fill(DS, "chtRevenue")
    End Using
End Sub

【讨论】:

以上是关于System.InvalidOperationException:“填充:SelectCommand.Connection 属性尚未初始化。”的主要内容,如果未能解决你的问题,请参考以下文章

视图中出现 InvalidOperationException.Collection 被修改错误