开发者

Can't connect to an online database using my window application. (C# winforms)

开发者 https://www.devze.com 2023-01-17 07:43 出处:网络
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. Ple开发者_运维问答ase

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. Ple开发者_运维问答ase 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?

By the way, this the error message being produced: Unable to connect to any of the specified MySQL hosts.

Can't connect to an online database using my window application. (C# winforms)


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.


Most online, shared hosting plans do not allow connections to databases from external clients, for security reasons.

You'll most likely need to use their tools to allow connections from your IP address, or use some form of tunneling to make the connection. For example, Dreamhost has instructions for using SSH to tunnel.

0

精彩评论

暂无评论...
验证码 换一张
取 消