【问题标题】:Cannot connect to database when not running on localhostC# 到网站数据库连接
【发布时间】:2017-10-26 22:22:06
【问题描述】:

美好的一天!

所以情况就是这样。我在 Visual Studio 2012 C# 中创建了一个简单的程序,它使用localhost 作为我的服务器连接到 MySQL 数据库。问题是,当我尝试将我的程序连接到我的网站(使用与我的本地主机相同的数据库)时,我收到了这个错误:

无法连接到任何指定的 MySQL 主机。

这是我到目前为止所做的:

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

再次,使用localhost(WampServer)作为服务器,我可以连接成功。但是当我使用我的网站作为在线服务器时,它会抛出上述错误。

根据我的研究,Web 服务器不允许远程访问其数据库。如果是这样,您能否给我其他选项来使用我的 C# 应用程序更新我的在线数据库。提前致谢!

【问题讨论】:

    标签: c# php mysql web server


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 是的,先生,我已经添加了我的 IP 地址,但错误仍然存​​在。 :-(
      • 您使用主机提供商的连接字符串吗?
      猜你喜欢
      • 2019-04-15
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 2016-05-23
      • 2013-09-06
      • 2013-02-16
      相关资源
      最近更新 更多