美好的一天!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#應用程序更新我的在線數據庫。先謝謝了!
是的,先生,我已經添加了我的IP地址,但錯誤仍然存在。 :-( – LoudSpeaker
您是否使用主機提供商提供的連接字符串? – 2017-05-26 06:07:52