Connection to MySQL issue - mysql

I would like to make a connection between my program in VB.NET and my sql database. The code is like this:
Public Function connecter()
Dim Connexion As String = "Server=197.28.178.33;Database=test;Uid=userid;Pwd=xxxxxxxxxpassxxxx;"
Dim conn As MySqlConnection = New MySqlConnection
conn.ConnectionString = Connexion
conn.Open()
Return conn
End Function
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lecteur As MySqlDataReader
Dim Requete As String
connecter.Close()
connecter()
Requete = "select * from article"
Dim Commande As New MySqlCommand(Requete, connecter)
lecteur = Commande.ExecuteReader
Do While lecteur.Read
ComboBox1.Items.Add(lecteur.GetString("description"))
Loop
connecter.Close()
End Sub
but this error appears:
An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException'
occurred in MySql.Data.dll
Additional information: Unable to connect to any of the specified
MySQL hosts.
what is the cause of this error?

I don't understand what is the problem and how I can verify if I communicate with the server? and How I'm sure that the server is running

Related

Cannot connect to remote database using mySQL on a form in VB.net

Trying to connect to a remote database on my hostinger.de account using VB.net. Connecting to the database using PHP on my hostinger.de website works just fine. Trying to connect via VB.net from desktop throws an error message:
a network-related error occurred... Win32Exception: The network path was not found."
I double-checked the server path. It is exactly as mentioned in my php admin area. Connecting via MySQL workbench works fine.
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myConn As SqlConnection
Dim myCmd As SqlCommand
Dim myReader As SqlDataReader
Dim results As String
myConn = New SqlConnection("server=sql588.main-hosting.eu;uid=u942496093_User1;database=u942496093_Test1;password=abcDEF123!")
'myConn = New SqlConnection("server=145.14.151.51;uid=u942496093_User1;database=u942496093_Test1;password=abcDEF123!")
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT Vorname, Nachname FROM TestTabelle1"
myConn.Open()
myReader = myCmd.ExecuteReader
Do While myReader.Read()
results = results & myReader.GetString(0) & vbTab &
myReader.GetString(1) & vbLf
Loop
MsgBox(results)
myReader.Close()
myConn.Close()
End Sub
End Class

What is the ConnectionString to connect MySQL db with VB.NET

I am working on to connect my App form VB.Net with MySQL table database with this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str As String = "data source=localhost;Initial Catalog=tabledb;user id=root;password=password1234"
Dim con As New SqlConnection(str)
con.Open()
Dim com As String = "Select * from springdata where rfid_tag='" & Rfid_tagTextBox.Text & "'"
Dim cm As New SqlCommand(com, con)
Dim rd As SqlDataReader = cm.ExecuteReader()
If rd.Read() = True Then
MessageBox.Show("Valid username and password")
Else
MessageBox.Show("Invalid username and password", caption:="login")
End If
End Sub
But when I run the app gave me this error:
Additional information: A network-related or instance-specific error
occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not
open a connection to SQL Server)
What is the correct ConnectionString to use with MySQL 5.7.
The connection string below is an SQL Server connection string:
Dim str As String = "data source=localhost;Initial Catalog=tabledb;user id=root;password=password1234"
According to MySQL Connector .NET connection string, you should provide server name, port number (if using different port), database name and credentials like this:
Dim str As String = "Server=localhost;Port=3306;Database=tabledb;Uid=root;Pwd=password1234"
Also you should use MySqlConnection, MySqlCommand & MySqlDataReader instances with parameters instead of value concatenation inside query string (ensure that reference to MySql.Data.dll added first):
Using con As New MySqlConnection(str)
con.Open()
Dim com As String = "Select * from springdata where rfid_tag=#tag"
Using cm As New MySqlCommand(com, con)
cm.Parameters.Add("#tag", MySqlDbType.VarChar).Value = Rfid_tagTextBox.Text
Dim rd As MySqlDataReader = cm.ExecuteReader()
' Check if any rows exist
If rd.HasRows = True Then
' do something
Else
' do something else
End If
End Using
End Using

Error connecting to SQL with VB

So i tried to connect my sql database to my vb project but it gives me this error:
Error Connecting to Database : Authentication to host "for user" using
method 'mysql_native_password' failed with message : Acces denied for
user "#'N56VB' (using password : NO)
N56VB is my pc
UPDATE the error wich I get now is : A first chance exception of type
'System.ArgumentException' occurred in System.Data.dll
Additional information: Keyword not supported.
If there is a handler for this exception, the program may be safely
continued
I dont know what to do anymore it will be a stupid I guess ,be easy on me i'm a noob :(
I hope someone knows the problem
Imports MySql.Data.MySqlClient
Imports System.Data.Sql
Imports System
Imports System.Data
Public Class NieuweItems
Public dbconn As New MySqlConnection("Data Source=localhost;user id=root;password=;database=gip;")
Dim conn As New MySqlConnection
Private Sub btn_return_Click(sender As Object, e As EventArgs) Handles btn_return.Click
Me.Close()
SalesApplication.Show()
End Sub
Private Sub btn_inlezen_Click(sender As Object, e As EventArgs) Handles btn_inlezen.Click
Dim sqlCommand As New MySqlCommand
Dim SQLConnection As MySqlConnection = New MySqlConnection
Dim strStockSQL As String
SQLConnection.ConnectionString = "Data Source= localhost ;user id = root'#'localhost; password = ;database=gip;table=stock;"
Try
conn.Open()
strStockSQL = "INSERT INTO Barcode " & txtBarcode.Text & "INSERT INTO Naam_Product" & txtNaam.Text & "INSERT INTO Verkoopprijs" & txtVP.Text
sqlCommand.ExecuteNonQuery()
SQLConnection.Close()
conn.Close()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
Finally
conn.Close()
End Try
End Sub
End Class
I presume that the error is occurring inside the "btn_inlezen_Click" event handler? I think what's happening is that you're declaring a new empty MySqlConnection, opening the connection inside your Try block and only then are you setting the ConnectionString. Because MySqlConnection was declared with no parameters it might be trying to use N56VB as the user account and this doesn't have any permissions on your MySQL db.
So set your connection string before opening the connection.

How to fix web connection failed to load data from my database

Im trying to create a simple database load connection through web page but it always give me an error: "A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
This is my web.config, using MySQL, MySQL server 2005
<connectionStrings>
<add name="MySQLConnection" connectionString="database=peronrecords;server=localhost;uid=root;pwd=root"/>
</connectionStrings>
And this is my vb code using gridview:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("MySQLConnection").ToString())
' Create a DataSet object.
Dim da As SqlDataAdapter
Dim dt As New DataTable
da = New SqlDataAdapter("SELECT id, name FROM student", conn)
da.Fill(dt)
' Open the connection
conn.Open()
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Sub
If you are connecting to MySql then you need to use the appropriate classes.
The SqlConnection and SqlDataAdapter are the classes required to connect to Microsoft Sql Server, for MySql you need the MySqlConnection and MySqlDataAdapter.
These classes could be found if you download and install the MySql NET Connector, then add a reference to MySql.Data.dll and add an Imports statement for MySql.Data.MySqlClient to the top of your code file.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Using conn As New MySqlConnection(ConfigurationManager.ConnectionStrings("MySQLConnection").ToString())
' Create a DataSet object'
Dim da As MySqlDataAdapter
Dim dt As New DataTable
da = New MySqlDataAdapter("SELECT id, name FROM student", conn)
da.Fill(dt)
' Not needed. The DataAdapter opens the connection by itself if it is not opened
' conn.Open()
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Sub

PC denied acces to connect to Database

Hi im relatively new to VB.NET so please bear with me here.
Im trying to setup a DB connection to my localhost everything looks good in my code except I get the error, when running the program, this PC is not allowed to connect to this DB, as you can see in the following image:
Here is my code I use to connect to the DB
Imports MySql.Data.MySqlClient
Public Class sreg
Dim ServerString As String = "Server=localhost;User Id=root;Password="";Database=vbdb"
Dim SqlConnection As MySqlConnection = New MySqlConnection
Private Sub sreg_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SqlConnection As MySqlConnection = New MySqlConnection
SqlConnection.ConnectionString = ServerString
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
MsgBox("Successfully connected to MySQL DB")
Else
SQLConnection.Close()
MsgBox("Connection is Closed")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SaveNames(ByRef SQLStatment As String)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLStatment
.CommandType = CommandType.Text
.Connection = SqlConnection
.ExecuteNonQuery()
End With
SqlConnection.Close()
MsgBox("Succesfully Added!")
SqLConnection.dispose()
End Sub
End Class
I suspect the problem might be in this line SqlConnection.ConnectionString = ServerString since above line returns error when I run the program, however when I remove it I get it to work, but get the not allowed to connect to DB error, as you can see in the image
It looks like you are trying to connect to a MySQL server and it looks like the connection string you need is slightly different than for a MSSQL server:
"Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
Source: https://www.connectionstrings.com/mysql/