Robis
Robis

Reputation: 5

C# - Can't connect to remote MySQL server

My problem is that I can't connect to my website remote MySQL server. I have read all answers in stackoverflow.com, but I can't find right answer. Here's my C# code:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication3
{
class Program
{
    static void Main(string[] args)
    {
        SqlConnection con;

        string connectionString = @"Server=[IP adress];Port=3306;Database=[database];Uid=[user];Pwd=[pass];"; 
        con = new SqlConnection(connectionString);
        try
        {
            con.Open();
            Console.WriteLine ("Connection Open ! ");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Can not open connection ! ");
            Console.WriteLine(ex.Message); //shows what error actually occurs
        }
        Console.ReadLine();
    }
}
}

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)

Any ideas?

Upvotes: 0

Views: 2108

Answers (2)

Sven Milewski
Sven Milewski

Reputation: 36

When connecting to a MySQL-Database I always used the MySQL Connector you can get here: https://dev.mysql.com/downloads/connector/net/6.9.html

You have to import the MySQL namespaces to your project and then you can use the MySQLConnection instead of the SQLConnection that is, as far as I know, only for MSSQL servers.

http://www.codeproject.com/Tips/423233/How-to-Connect-to-MySQL-Using-Csharp

Upvotes: 2

Spider man
Spider man

Reputation: 3330

try the following

string connectionString = @"Server=[IP adress]:3306;Database=[database];Uid=[user];Pwd=[pass];";

instead of

 string connectionString = @"Server=[IP adress];Port=3306;Database=[database];Uid=[user];Pwd=[pass];";

Upvotes: 0

Related Questions