2017-05-26 112 views
2

美好的一天!C#到網站數據庫連接

所以這裏的情況。我在Visual Studio 2012 C#中創建了一個簡單的程序,它使用localhost作爲我的服務器連接到MySQL數據庫。問題是,當我嘗試我的程序連接到我的網站(使用同一數據庫中我的本地主機),我得到這個錯誤:

Unable to connect to any of the specified MySQL hosts.

這是我到目前爲止已經完成:

 private void cmdLoadTable_Click(object sender, EventArgs e) 
    { 
     string strServer = "localhost"; 
     string strPort = "3306"; 
     string strDatabase = "myDatabase"; 
     string strUser = "myUser"; 
     string strPassword = "myPassword"; 

     MySqlConnection iConnect; 
     iConnect = new MySql.Data.MySqlClient.MySqlConnection("server=" + strServer + "; port=" + strPort + "; database=" + strDatabase + "; user id=" + strUser + "; password=" + strPassword); 

     iConnect.Open(); 

     MySqlCommand iCommand = new MySqlCommand("SELECT * from Table1", iConnect); 
     MySqlDataAdapter iAdapter = new MySqlDataAdapter(iCommand); 
     DataSet iDataSet = new DataSet(); 
     iAdapter.Fill(iDataSet, "LIST"); 

     dataGridView1.ReadOnly = true; 
     dataGridView1.DataSource = iDataSet.Tables[0]; 
     } 

再次,使用本地主機(WampServer)作爲服務器,我可以連接它成功。但是當我使用我的網站作爲在線服務器時,它會拋出上述錯誤。

根據我的研究,網絡服務器不允許遠程訪問他們的數據庫。如果是這樣,你可以給我其他選擇,以使用我的C#應用​​程序更新我的在線數據庫。先謝謝了!

回答

0

Ussualy,您需要將您的IP地址添加到服務器上的white-list

+1

是的,先生,我已經添加了我的IP地址,但錯誤仍然存​​在。 :-( – LoudSpeaker

+0

您是否使用主機提供商提供的連接字符串? – 2017-05-26 06:07:52

0

檢查您的防火牆配置或其他安全設置。