2016-05-22 26 views
1

我試圖連接到一個免費的在線數據庫(db4free.net),我不能在程序試圖打開連接時拋出KeyNotFoundException。KeyNotFoundException與在線數據庫

這裏是我的代碼:

public static class BaseDeDatos { 
    static string myConnectionString = "SERVER=db4free.net;" + "DATABASE=******;" + "UID=******;" + "PASSWORD=*****;"; 
    static MySqlConnection cnx = new MySqlConnection(myConnectionString); 

      static String error; 

      public static int Conectar() 
      { 
       int resultado = -1; 
       try 
       { 
        cnx.Open(); //The exception is thrown HERE 
        if (cnx.State == ConnectionState.Open) 
         resultado = 1; 
       } 
       catch (MySqlException ex) 
       { 
        error = ex.Message; 
       } 
       return resultado; 
      } 

      . 
      . 
      . 
      . 
      public static void myMethod(){ 

       Conectar(); 

     } 
     } 

當我打電話myMethod()拋出異常。我可以將此數據庫與MySQLWorkbench連接,並連接到PHP Android,但爲什麼我無法在Visual Studio中使用C#執行此連接?我敢肯定,我做錯了什麼幫助我,請

PD:我讀的是我需要更改我的數據庫的字符集,但它不起作用。我想所有的字符集可獲取

+0

什麼是確切的錯誤信息?你安裝了[Connector/Net](http://dev.mysql.com/downloads/connector/net/)嗎? –

+0

是的,錯誤來自連接字符串,因爲我使用這個連接字符串到其他數據庫,它適用於我,但我認爲,db4free有一個differente連接字符串 – cnbandicoot

回答

1

望着如Connecting to a Online MySQL Database using VB.Net

MySQLConnection.ConnectionString = "server=db4free.net;Port=3306; User ID=db4freeusername; password=db4freepassword; database=nameofyourdatabase" 

其他例子他們指定端口號,可這是你的問題?

+0

不,這不是解決方案。我嘗試了很多組合來創建連接字符串,並且我不知道我需要使用 – cnbandicoot

+0

OK,嘗試使用我的連接字符串static string myConnectionString =「SERVER = db4free.net; DATABASE = omega900; UID = chris_csjt; PASSWORD = P @提供ssword;「; ,看看它是否適用於你的代碼。它從這裏起作用 – OldBoyCoder

+0

第一種解決方案是正確的朋友。我不知道是什麼問題,但我創建了一個新的數據庫,並且它可以工作。我猜這些設置在數據庫中是錯誤的。謝謝。 – cnbandicoot