2015-06-22 150 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];";