我注册了一个免费虚拟主机帐户。 该站点提供MySQL数据库,我试图使用我的Windows应用程序访问它,但我无法连接到服务器。请考虑我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace MySql
{
public partial class Form1 : Form
{
BackgroundWorker bg = new BackgroundWorker();
string MyConString = "SERVER=209.51.195.117;" + // MySQL host: sql100.0fees.net
"DATABASE=mydb;" + // IP: 209.51.195.117
"UID=myusername;" + // not really my username
"PASSWORD=mypassword;"; // and password
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
}
void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
}
void bg_DoWork(object sender, DoWorkEventArgs e)
{
string status = String.Empty;
MySqlConnection connection = new MySqlConnection(MyConString);
try
{
connection.Open();
if (connection.State == ConnectionState.Open)
lblStatus.Text = "Connected";
else
lblStatus.Text = "No connection";
}
catch (Exception x)
{
lblStatus.Text = x.Message;
}
}
private void button1_Click(object sender, EventArgs e)
{
lblStatus.Text = "Connecting... please wait.";
button1.Enabled = false;
bg.RunWorkerAsync();
}
}
}
是否可以将我的应用程序连接到在线数据库?或者我的代码中有错误吗?
顺便说一下,这会产生错误消息:Unable to connect to any of the specified MySQL hosts.
答案 0 :(得分:1)
除非您完全控制服务器,否则无法将mysql服务公开给Web。出于安全原因,这种限制是由(几乎;可能有例外)所有免费托管商强加的。除非您在服务器上提供访问数据的Web服务,否则无法远程访问数据库。
答案 1 :(得分:0)
出于安全原因,大多数在线共享主机方案不允许从外部客户端连接到数据库。
您很可能需要使用他们的工具来允许来自您的IP地址的连接,或者使用某种形式的隧道来建立连接。例如,Dreamhost has instructions for using SSH to tunnel。