简体   繁体   English

C#到网站数据库连接

[英]C# to Website Database Connection

Good day! 美好的一天!

So here's the situation. 这就是这种情况。 I created a simple program in Visual Studio 2012 C# that connects to MySQL Database using localhost as my server. 我在Visual Studio 2012 C#中创建了一个简单程序,该程序使用localhost作为服务器连接到MySQL数据库。 The problem is, when I try to connect my program to my website (with the same database as my localhost), I'm getting this error: 问题是,当我尝试将程序连接到我的网站(使用与本地主机相同的数据库)时,出现此错误:

Unable to connect to any of the specified MySQL hosts. 无法连接到任何指定的MySQL主机。

Here is what I've done so far: 到目前为止,这是我所做的:

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

Again, using localhost (WampServer) as a server, I can connect it successfully. 同样,使用本地主机(WampServer)作为服务器,我可以成功连接它。 But when I use my website as server online, it throws the said error. 但是,当我将我的网站用作在线服务器时,它会抛出上述错误。

According to my research, web servers didn't allow remote access to their databases. 根据我的研究,Web服务器不允许对其数据库的远程访问。 If so, can you please give me other options to update my online database using my C# application. 如果是这样,您能否给我其他选择,以便使用我的C#应用​​程序更新我的在线数据库。 Thanks in advanced! 提前致谢!

通常,您需要将IP地址添加到服务器的white-list中。

检查防火墙的配置或其他安全设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM