gpt4 book ai didi

mysql - 图表与MySql的连接

转载 作者:行者123 更新时间:2023-11-29 09:29:22 30 4
gpt4 key购买 nike

我正在连接 mysql 数据库中的图表对象,但收到此错误:

object reference not set to an instance of an object

这是我使用的代码:

Imports MySql.Data.MySqlClient
Public Class Form1
Dim con As New MySqlConnection
Dim com As MySqlCommand
Dim dt As New DataTable

Private Sub btnlaod_Click(sender As Object, e As EventArgs) Handles btnlaod.Click
con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=;database=noh_mis"
Dim Reader As MySqlDataReader

Try
con.Open()
Dim query As String
query = "Select * From database.students_profile"
Reader = com.ExecuteReader
While Reader.Read
Chart1.Series("Male").Points.AddXY(Reader.GetString("Gender"), Reader.GetString("GradeLevel"))
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try

End Sub

End Class

最佳答案

在使用com.ExecuteReader之前,您没有发出命令。您创建了一串 sql,但从未将其与数据库连接。

尝试在构建查询之后、Reader = com.ExecuteReader之前添加以下行:

com = New MySqlCommand(query, con)

关于mysql - 图表与MySql的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59025260/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com