简体   繁体   English

VB.NET SqlConnection到MySQL数据库

[英]VB.NET SqlConnection to MySQL Database

although I did read plenty of stackoverflow posts and also checked out lots of tutorial websites I can't get my code to work. 尽管我确实阅读了很多stackoverflow的文章,还检查了许多教程网站,但我的代码无法正常工作。

I want to use sqlBulkCopy. 我想使用sqlBulkCopy。 And therefor I need a instance of the 'SqlConnection' class. 因此,我需要'SqlConnection'类的实例。 I have a MySQL Database running and I have no problem at all opening a connection with the 'MySqlConnection' class. 我有一个正在运行的MySQL数据库,打开与“ MySqlConnection”类的连接完全没有问题。

I have tried several approaches to get SqlConnection working. 我尝试了几种方法来使SqlConnection工作。

MySqlConnection (works): MySqlConnection(有效):

  Dim a = "Server=localhost;Initial Catalog=hauptdatenbank;User Id=johnny;Password=123456;"

  Using cn As New MySqlConnection(a)
     cn.Open()
  End Using

SqlConnection (does not work): SqlConnection(不起作用):

  Dim a = "Server=localhost;Initial Catalog=hauptdatenbank;User Id=johnny;Password=123456;"

  Using cn As New SqlConnection(a)
     cn.Open()
  End Using

Following SqlException gets thrown: 引发以下SqlException:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

SqlBulk is only for a SQL DB. SqlBulk仅适用于SQL DB。 MySqlBulkLoader is for a MySQL DB. MySqlBulkLoader用于MySQL数据库。

Thanks for the comments! 感谢您的评论!

Imports MySql.Data.MySqlClient 导入MySql.Data.MySqlClient

Module Connection 模块连接

Public str1 As String = "data source=localhost;user id= root;password=;database= parisbugdb;"

End Module 终端模块

If Len(Trim(PassBox.Text)) = 0 Then
    AlertLabel.Visible = True
    AlertLabel.Text = "Password required!"
    PassBox.SelectAll()
    PassBox.Focus()
    Return
End If

con.Open()
Dim Sql = "SELECT * FROM userx WHERE username ='" & UserBox.Text & "' AND password = '" & PassBox.Text & "'"
scmd = New MySqlCommand(sql, con)
Dim sdr As MySqlDataReader = scmd.ExecuteReader


Try
    If sdr.Read = False Then

        AlertLabel.Visible = True
        AlertLabel.Text = "Login denied!"
        con.Close()
    Else

        UserBox.Clear()
        PassBox.Clear()
        UserBox.Focus()
        Me.Hide()
        Dashboard.Show()
    End If


Catch ex As Exception
    MsgBox(ex.ToString())
End Try

End Sub 结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM