Cannot connect to a network mysql database - mysql

I am building a Windows Store application who is intended to interact with a MySql database located at a server in my local network.
I am using MySQL connector/net RT to connect(which works on my testing localhost DB), the problem is that for some reason i cannot connect to the network database; although i can get a successful connection with a different Win32 app, who has the same connection string.
i disabled the firewall, i created a new user on the host to access from my AP, etc. Can somebody tell me what am I doing wrong?
C#
using MySQL.Data.MySqlClient;
using Windows.UI.Popups;
MySqlConnection connection = new MySqlConnection("server=1.1.1.1;port=3306;database=mydb;uid=user;password=****;");
try{
connection.Open();
connection.Close();
}
catch (MySqlException e){
MessageDialog md = new MessageDialog(e.ToString());
md.ShowAsync();
}
//Exception:
failed to Open Connection

This is how I connect. Hope it Helps
Imports MySql.Data.MySqlClient
Public Class ClassMySqlConnect
Public Shared SQLConnect As String = "Server=YOURSERVERADDRESS; User Id=YOURID; Password=YOURPASSWORD;Database=YOURDATABASENAME"
Public Function connect(ByVal PlayerID As String) As String
Dim SQLConnection = New MySqlConnection
SQLConnection.ConnectionString = SQLConnect
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
'Return "Sucessfully Connected to MySql Database"
Else
SQLConnection.Open()
' Return "Connection is closed"
End If
Catch ex As Exception
' Return ex.ToString()
End Try
Dim SQLStatement As String = "INSERT INTO Toons (PlayerID) Values('" + PlayerID + "')"
Dim cmd As New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
Return "successfully saved"
End Function
End Class

Related

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

got an error message unable to connect to any of the specified mysql hosts in vb.net

i want to connect with mysql phpmyadmin database and get data in my application in vb.net but i can not connect.i got error message that unable to connect with specified host.
when i entered 10.1.1.53/phpmyadmin in my browser i am able to open my all database without any password or username but when i try to connect with my vb.net application by clicking button i got error message.
this is my code. please help me.
ImportsMySql.Data.MySqlClient
PublicClasslogin
Dim con AsMySqlConnection
Dim reader AsMySqlDataReader
Dim command AsMySqlCommand
DimmyconAsString = "server=10.1.1.53/phpmyadmin;database=dbtest"
PrivateSubbtnlogin_Click(sender AsObject, e AsEventArgs) Handlesbtnlogin.Click
login()
EndSub
PrivateSublogin()
con = NewMySqlConnection
con.ConnectionString = mycon
Try
con.Open()
Catch ex As Exception
EndTry
EndSub
PrivateSubbtnexit_Click(sender AsObject, e AsEventArgs) Handlesbtnexit.Click
Me.Close()
EndSub
EndClass
Public Sub login()
Dim DatabaseName As String = "dbtest"
Dim server As String = "10.1.1.53"
Dim userName As String = "username"
Dim password1 As String = "pass"
If Not conn Is Nothing Then conn.Close()
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password1, DatabaseName)
Try
conn.Open()
' MsgBox("Connected")
Catch ex As Exception
MsgBox(ex.Message)
End Try
'conn.Close()
End Sub
the one that u gave me is DimmyconAsString << is that typo?

VB Insert into MySql

As a Vb noob im working on this school project. I need to insert my values into my mysql database but for a reason it isn't inserting tried everything but i can't find why it isn't inserting.
Thx in advance
Dim sqlCommand As New MySqlCommand
Dim SQLConnection As MySqlConnection = New MySqlConnection
Dim strStockSQL As String
Dim server As String = "localhost"
Dim DatabaseName As String = "Gip"
Dim userName As String = "root"
Dim password As String = ""
SQLConnection = New MySqlConnection()
If Not conn Is Nothing Then conn.Close()
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
Try
strStockSQL = "insert into stock (Barcode,Naam_Product,Verkoopprijs) values (#Barcode,#Naam_product,#Verkoopprijs)"
sqlCommand.Connection = SQLConnection
sqlCommand.CommandText = strStockSQL
sqlCommand.Parameters.AddWithValue("#Barcode", Convert.ToString(txtBarcode.Text))
sqlCommand.Parameters.AddWithValue("#Naam_product", Convert.ToString(txtNaam.Text))
sqlCommand.Parameters.AddWithValue("#Verkoopprijs", Convert.ToInt32(txtVP.Text))
sqlCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Error occured: Could not insert record")
When executing an sqlCommand you must have it's related connection object in open state.
SQLConnection.Open()
sqlCommand.ExecuteNonQuery()
SQLConnection.Close()
Also, read about Using statement and use it for SqlConnection.
Another thing: this code line is meaningless: If Not conn Is Nothing Then conn.Close() remove it.

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/

mysql using vb.net?

I developed a project in VB.NET
In this project I want to use data from MySQL that is resides in my WEB Server.
I can communicate with the MySQL server of my localhost but can not communicate with the WEB Server.
In my CPanel I added Host from the Remote Database Access
But I can't communicate with WEB MySQL Server.
Please help me.
Dim connection As MySqlConnection
connection = New MySqlConnection()
connection.ConnectionString = "Server=saver ip; Port=2082; Uid=username; Pwd=password; Database=database; Connect Timeout=60;"
Try
connection.Open()
MessageBox.Show("Connection Opened Successfully")
connection.Close()
Catch mysql_error As MySqlException
MessageBox.Show("Error Connecting to Database: " & mysql_error.Message)
Finally
connection.Dispose()
End Try
When i try to run this. I got this error "Error Connecting to Database: Reading from the stream has failed."
Note*: My database name like "myweb_dbname" and my user name "myweb_username" is this ok? i am using cPanal1.0 (RC1) and mysql5.1.56-log and os linux.
Jeff V : Thank you! When i try your code..
Dim dataConnection As New MySql.Data.MySqlClient.MySqlConnection()
dataConnection.ConnectionString = "Server = xx.xx.xxx.xxx; Database = dbNAME; Uid = userID; Pwd = password;"
Dim dataCommand As MySql.Data.MySqlClient.MySqlCommand = New MySqlCommand()
dataCommand.Connection = dataConnection
Try
dataConnection.Open()
dataConnection.Close()
Catch x As Exception
Console.WriteLine(x.Message.ToString())
MsgBox(x.ToString)
End Try
I Get this error message:
MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the
specified MySQL hosts. at
MySql.Data.MySqlClient.NativeDriver.Open() at
MySql.Data.MySqlClient.Driver.Open() at
MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder
settings) at
MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() at
MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() at
MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() at
MySql.Data.MySqlClient.MySqlPool.GetConnection() at
MySql.Data.MySqlClient.MySqlConnection.Open() at
mysql.Form1.Button3_Click(Object sender, EventArgs e) in
C:\Users\pram\Documents\Visual Studio
2008\Projects\mysql\mysql\Form1.vb:line 48
In line 48 "dataConnection.Open()"
I am not sure about what is going on down there, but if this is MSSQL, I would normally check with protocols, firewall
try this one. I am using this snippet to execute commands.
Imports MySql.Data.MySqlClient
Dim strConnString As String = "server=xxx.x.x.x;uid=user;pwd=password;database=xxx"
Dim objConn As New MySqlConnection(strConnString)
Public Function MyDataSet(ByVal strSQL As String) As DataSet
Dim ds As New DataSet
Dim mycmd As MySqlCommand = New MySqlCommand(strSQL, objConn)
Dim ad As MySqlDataAdapter = New MySqlDataAdapter
ad.SelectCommand = mycmd
objConn.Open()
ad.Fill(ds, "a")
objConn.Close()
Return ds
End Function
Best Regards,
#iamsupergrasya
This is how I did it (using C#):
using System.Data.Sql;
using MySql.Data.MySqlClient;
MySql.Data.MySqlClient.MySqlConnection dataConnection = new MySql.Data.MySqlClient.MySqlConnection();
dataConnection.ConnectionString = "Server = xx.xx.xxx.xxx; Database = dbNAME; Uid = userID; Pwd = password;";
MySql.Data.MySqlClient.MySqlCommand dataCommand = new MySqlCommand();
dataCommand.Connection = dataConnection;
//tell the compiler and database that we're using parameters (thus the #first, #last, #nick)
dataCommand.CommandText = ("INSERT INTO ...;");
//add our parameters to our command object
dataCommand.Parameters.AddWithValue("#recordId", dataString[0].ToString());
...
try{
dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();
}catch (Exception x){
Console.WriteLine(x.Message.ToString());
}
I thought I might of had it originally in VB.Net but you "should" be able to convert this to VB fairly easily. If you still need help let me know. I will try to convert it tonight.
Let me know if it works for you.
UPDATE - VB.NET Code:
Dim dataConnection As New MySql.Data.MySqlClient.MySqlConnection()
dataConnection.ConnectionString = "Server = xx.xx.xxx.xxx; Database = dbNAME; Uid = userID; Pwd = password;"
Dim dataCommand As MySql.Data.MySqlClient.MySqlCommand = New MySqlCommand()
dataCommand.Connection = dataConnection
'tell the compiler and database that we're using parameters (thus the #first, #last, #nick)
dataCommand.CommandText = ("INSERT INTO ...;")
'add our parameters to our command object
dataCommand.Parameters.AddWithValue("#recordId", dataString(0).ToString())
Try
dataConnection.Open()
dataCommand.ExecuteNonQuery()
dataConnection.Close()
Catch x As Exception
Console.WriteLine(x.Message.ToString())
End Try
I used this converter tool (http://www.developerfusion.com/tools/convert/csharp-to-vb/) - So try this out.
Update - Answer from question on comment:
you can ping your site using the command prompt:
cmd.exe
Then in the command window type in the URL of your site. That will give you back the IP address of your site.