2015-06-22 154 views
0

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

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

_ “任何想法?” _ - '不支持关键字:“port'.'我的猜测是,在'port'关键字不被支持。你是否尝试过连接字符串而不指定端口? – DGibbs

+0

确实:无论如何3306是默认的端口,所以请尝试删除它。 –

+0

我已经向我的托管公司寻求帮助,所以他们说我需要使用端口3306 – Robis

回答

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

+0

错误:无法连接到任何指定的MySQL主机。 – Robis

+0

MySQLConnection的语法可能与SQLConnection不同。 http://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-connection.html –

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];";