未选择数据库MySQL数据库已连接到VS 2017 Community Edition(vb.net)

时间:2018-07-10 02:52:02

标签: mysql visual-studio

我试图搜索堆栈溢出,但发现了一个帖子,但该解决方案对我不起作用。

我正在使用MySql数据库,该数据库已连接到Visual Studio 2017 Professional(社区版)软件,并且项目是用VB.net编写的。我必须安装以下扩展:-适用于Visual Studio 1.2.7的MySQL和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()

在此先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试这个

作为新MySqlConnection的昏暗连接(“ server = localhost; Port = 3306; database = mydatabase; username = root; Password = mypassword”)