gpt4 book ai didi

mysql - 没有选择数据库 VS 2017社区版(vb.net)连接的MySQL数据库

转载 作者:行者123 更新时间:2023-11-29 17:21:01 24 4
gpt4 key购买 nike

我尝试搜索堆栈溢出并找到了一篇文章,但该解决方案对我不起作用。

我正在使用连接到 Visual Studio 2017 Professional(社区版)软件的 MySql 数据库,该项目是用 VB.net 编写的。我必须安装以下扩展: - MySQL for Visual Studio 1.2.7 和 MySQL ConnectorNet 8.0.11。为了连接MySQL数据库,应确保数据源是MySQL数据库。我已成功将数据从 MySql 数据库加载/查看到 DataGridView 中。源代码如下:-

Imports MySql.Data.MySqlClient
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports MySql.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System
Public Class Form1

Dim connection As New MySqlConnection("server=localhost; Port=3306; username=root; Password=mypassword")
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Private connStr As String
Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles
SubmitButton.Click

Dim dtEmployee As New DataTable()

Dim command As New MySqlCommand("SELECT * FROM Employees WHERE EmployeeDate BETWEEN @d1 And @d2", connection)

command.Parameters.Add("@d1", MySqlDbType.Date).Value = DateTimePicker1.Value
command.Parameters.Add("@d2", MySqlDbType.Date).Value = DateTimePicker2.Value

Dim adapter As New MySqlDataAdapter(command)

adapter.Fill(dtEmployee)

DataGridView.DataSource = dtEmployee

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

DataGridView.DataSource = GetEmployeeList()

End Sub

Private Function GetEmployeeList() As DataTable

Dim dtEmployee As New DataTable

Dim connString As String = ConfigurationManager.ConnectionStrings("dbx").ConnectionString

Using conn As New MySqlConnection(connString)

Using cmd As New MySqlCommand("SELECT * FROM Employees", conn)

conn.Open()

Dim reader As MySqlDataReader = cmd.ExecuteReader()

dtEmployee.Load(reader)

End Using

End Using

Return dtEmployee

End Function
End Class

App.config 文件中的代码是:-

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<connectionStrings>
<add name="dbx" connectionString
="server=localhost;port=3306;database=mydatabase;user
id=root;password=mypassword" providerName="MySql.Data.MySqlClient"/>

我现在遇到一个问题,因为我无法过滤 Datagridview 中特定数据范围之间的数据。当我将日期范围设置在两个日期之间并单击提交按钮时,会在该行抛出“未选择数据库”错误消息:-

adapter.Fill(dtLab)

和错误消息:-

Exception Thrown
MySql.Data.MySqlClient.MySqlException: 'No database selected'
MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Message=No database selected
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64&
insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&
affectedRows, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()

提前感谢您的帮助

最佳答案

试试这个

将连接调暗为新的 MySqlConnection("server=localhost;Port=3306;database=mydatabase;username=root;Password=mypassword")

关于mysql - 没有选择数据库 VS 2017社区版(vb.net)连接的MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51256606/

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