1

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()"

9
  • I have code that talks to a MySQL db from .NET... But it is at home. If you don't get a suitable answer by tonight I'll find it and post it. Commented Feb 21, 2012 at 17:24
  • still no working answer plz.... help me...some one? Commented Feb 22, 2012 at 13:11
  • It sounds like you may need to look at your access hosts on your "Remote Database Access Hosts" screen. This might need to be updated if your IP address has recently changed (dynamic). Have you ever connected to your DB from SQL GUI (such as HeidiSQL)? Once I got that dialed in the code below worked. Commented Feb 23, 2012 at 21:12
  • @JeffV yes i am try to connected using SQL GUI(MySQL Workbench 5.2 CE) it say's "lost connection to MYSQL sever at 'reading initial communication packet',system error:0" and also i am added my ip in remote database access host like "xx.xx.%.%" and like this to "%.%.%.%" still not connecting. help?? Commented Feb 24, 2012 at 4:24
  • have you ever been able to connect remotely from the MySQL Workbench? If not contact support for your hosting provider. Get that situated first and then do the code. If you connect remotely then doing it in code won't be a problem. Commented Feb 24, 2012 at 16:46

3 Answers 3

0

I am not sure about what is going on down there, but if this is MSSQL, I would normally check with protocols, firewall

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Thank you! But it got a run time error at "objConn.Open()" line it say's "Unable to connect to any of the specified MySQL hosts." why??
0

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.

3 Comments

Thank you! But sorry! i am new to C# can you please convert this code in to vb.net?
Thank you! i try your code with 2 type with sql code with out sql code but not working!!! sorry! any idea??
are you referencing: System.Data.Sql and MySql.Data.MySqlClient

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.