无法连接到使用我的窗口的应用程序的在线数据库。 (C#的WinForms) [英] Can't connect to an online database using my window application. (C# winforms)
问题描述
我注册了一个免费的网络托管帐户。
该网站提供的MySQL数据库,我想用我的Windows应用程序来访问它,但我无法连接到服务器。请考虑我的代码:
I signed up for an account for a free web hosting. The site provides MySQL databases and I'm trying to access it using my windows application but I can't connect to the server. Please consider my code:
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();
}
}
}
有没有可能接我适用于在线数据库? ?或者有没有在我的代码中的错误
Is it possible to connect my application to an online database? Or are there errors in my code?
顺便说一句,这正在生产的错误消息:无法连接到任何指定的MySQL主机
By the way, this the error message being produced: Unable to connect to any of the specified MySQL hosts.
推荐答案
除非你有对服务器的完全控制,你不能暴露mysql服务到网上。 (;有可能是例外差不多)出于安全原因,所有的免费托管这一限制被强加的。除非你提供服务器访问数据的Web服务,则无法远程访问数据库。
Unless you have full control over the server, you can't expose the mysql service to the web. This restriction is imposed by (almost; there might be exceptions) all free hosters for security reasons. Unless you provide a webservice on the server to access data, you can't access the database remotely.
这篇关于无法连接到使用我的窗口的应用程序的在线数据库。 (C#的WinForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!