C# - 无法连接到远程MySQL服务器

时间:2015-06-22 10:40:40

标签: c# mysql

我的问题是我无法连接到我的网站远程MySQL服务器。我已经在stackoverflow.com上阅读了所有答案,但我无法找到正确的答案。这是我的C#代码:

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();
    }
}
}

错误:

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)

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

当连接到MySQL数据库时,我总是使用你可以到达的MySQL连接器:https://dev.mysql.com/downloads/connector/net/6.9.html

你必须将MySQL命名空间导入你的项目,然后你可以使用MySQLConnection而不是SQLConnection,据我所知,只有MSSQL服务器。

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

答案 1 :(得分:0)

尝试以下

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

而不是

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