c# - 将 C# Windows 窗体应用程序连接到基于 Linux 的服务器上的 MySQL 数据库

标签 c# php mysql linux

我正在开发一个 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;
            }


        }
    }
}

提前致谢..

最佳答案

我已经为我的 mysql 连接编写了一个带有函数的自己的类。

首先声明服务器连接:

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

关于c# - 将 C# Windows 窗体应用程序连接到基于 Linux 的服务器上的 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893350/

相关文章:

c# - 如何在 c# asp.net 中播放 twilio 通话录音

php - 在sql中查找与html按钮的id具有相同id的记录并生成模态

mysql - 如何通过排序获取查询结果顶部的特定值?

mysql - INSERT ... ON DUPLICATE KEY(什么都不做)

c# - 如何允许 .NET Core 控制台应用程序通过 Windows 防火墙进行 FTP 连接?

c# - 用反射“类型转换”

c# - 我如何告诉 mono 在哪里可以找到图书馆?

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

php - 隐藏html5/javascript代码的方法

javascript - 如何使用 AJAX 从 jquery 访问数据库单元