【问题标题】:Connect a C# Windows Form App to a MySQL Database on a Linux based server将 C# Windows 窗体应用程序连接到基于 Linux 的服务器上的 MySQL 数据库
【发布时间】:2015-10-31 19:41:33
【问题描述】:

我正在开发一个 C# 应用程序,该应用程序将使用位于我的网站中的远程 MySQL 数据库,该数据库托管在支持 PHP 和 MySQL 的 Linux 服务器上。

我尝试使用 MySql.Data.MySQLClient 参考直接连接到 MySQL 数据库,结果,我的程序抛出以下异常:

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

我搜索了很多教程,但没有找到解决方案...遇到同样的错误。

谁能告诉我怎么做。

请提出任何在线链接或教程或您自己的想法。

请帮帮我。

这是我的代码:

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 mysq
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {              
                string conn = "Server = myserver;database = db ;uid = username ;password = pwd ;";

                MySqlConnection con = new MySqlConnection(conn);
                con.Open();
                if (con.State == ConnectionState.Open)
                {
                    MySqlCommand cmd = new MySqlCommand("Select * from table", con);
                    DataTable dt = new DataTable();
                    MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
                    ad.Fill(dt);
                    dataGridView1.DataSource = dt;
                }

           }
            catch (Exception)
            {
                throw;
            }


        }
    }
}

提前谢谢..

【问题讨论】:

    标签: c# php mysql linux


    【解决方案1】:

    我已经为我的 mysql-connection 编写了一个包含函数的自己的类。

    首先声明服务器连接:

    string ServerConnection = "Server=localhost;Port=1234;Database=YourDb;Uid=user;password=pass;"
    

    这是我选择数据的方式:

    public static void DB_Select(string s, params List<string>[] lists)
            {
                try
                {
                    using(var conn = new MySqlConnection(ServerConnection))
                    {
                        conn.Open();
                        MySqlCommand cmd = conn.CreateCommand();
                        cmd.CommandType = CommandType.Text;
                        string command = s;
                        cmd.CommandText = command;
                        using(var sqlreader = cmd.ExecuteReader())
                        while (sqlreader.Read())
                        {
                            if (sqlreader[0].ToString().Length > 0)
                            {
                                for (int i = 0; i < lists.Count(); i++)
                                {
                                    lists[i].Add(sqlreader[i].ToString());
                                }
                            }
                            else
                            {
                                foreach (List<string> save in lists)
                                {
                                    save.Add("/");
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error while selecting data from database!\nDetails: " + ex);
                }
            }
    

    来电:

    List<string> allRows = new List<string>();
    
    DB_Select("SELECT field1 FROM table1 WHERE 1", allRows);
    

    注意:对于您选择的每个字段,您必须传递一个自己的列表,该列表将填充输出。


    对于更新您的数据库,这将是完全相同的功能。您只是不必对输出进行分级。可能看起来像这样:

    public static void DB_Update(string s)
            {
                try
                {
                    using (var conn = new MySqlConnection(ServerConnection))
                    {
                        conn.Open();
                        MySqlCommand cmd = conn.CreateCommand();
                        cmd.CommandType = CommandType.Text;
                        string command = s;
                        cmd.CommandText = command;
                        int numRowsUpdated = cmd.ExecuteNonQuery();
    
                        if(numRowsUpdated < 0)
                        {
                            MessageBox.Show("Warning DB-Contact: Affected_rows < 0!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("Updating database failed!\n\nSQL: {0}\n\nERROR: {1}",s,ex.Message));
                }
            }
    

    【讨论】:

      【解决方案2】:

      您必须在您的程序中提供正确的连接字符串,我怀疑您正在传递您托管数据库的服务器的正确 IP 地址或主机名。作为第一步,尝试 ping 数据库服务器以确保可以从您的计算机访问它。如果可以访问,请尝试以下操作:

      MySql.Data.MySqlClient.MySqlConnection conn;
      string myConnectionString;
      string conn = "Server =<provide proper ip address >;database = db ;uid = username ;password = pwd ;";
      
      conn = new MySql.Data.MySqlClient.MySqlConnection();
      conn.ConnectionString = myConnectionString;
      conn.Open();
      

      更多细节在这里:http://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html

      【讨论】:

      • 我复制并粘贴了网络主机提供的主机名和其他详细信息,得到同样的错误
      • 能ping通主机名吗?
      • No 无法 ping 请求超时
      • 那怎么连接呢?查找可访问的 IP 地址或主机名
      • 我使用的是免费域,不幸的是他们不允许远程访问我的 sql,我认为这就是原因.. 感谢您的支持 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 2017-01-17
      • 2012-04-03
      • 2015-05-27
      相关资源
      最近更新 更多