You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu与Win10环境下C#.Net程序连接MySQL数据库的技术问询

Hey there, let's work through getting your C# TCP server connected to MySQL on your Ubuntu DigitalOcean droplet running Mono. First off, using ADODB here is probably the root of any issues you're hitting—ADODB is a COM-based library that doesn't play nicely with cross-platform Mono setups. Let's fix this the right way:

1. Swap ADODB for the Official MySQL Connector/NET

The MySQL Connector/NET is the fully supported, cross-platform driver for connecting .NET/Mono apps to MySQL. It's way more reliable than ADODB in this environment.

  • Install it on your Ubuntu server first:
    sudo apt-get install libmysql6.1-cil
    
    If you're using a project file (like .csproj), you can also add the MySql.Data package via NuGet for easier dependency management.
2. Rewrite Your MySQL Connection Code

Here's a refactored version of your MySQL class using the official driver—this replaces your ADODB logic with something that works seamlessly on Mono:

using System;
using MySql.Data.MySqlClient; // Include this namespace

namespace BlackjackServer
{
    class MySQL
    {
        private MySqlConnection _dbConnection;

        public void MySQLInit()
        {
            try
            {
                // Adjust this connection string with your MySQL credentials
                string connectionString = "server=localhost;userid=your_db_user;password=your_db_pass;database=your_db_name;SslMode=None";
                
                _dbConnection = new MySqlConnection(connectionString);
                _dbConnection.Open();
                Console.WriteLine("Successfully connected to MySQL!");
            }
            catch (MySqlException ex)
            {
                Console.WriteLine($"MySQL Connection Error: {ex.Message}");
                // Add error handling here (like retry logic or logging)
            }
        }

        // Example method to fetch data (replace with your Recordset use case)
        public void FetchSampleData()
        {
            // Reconnect if the connection is closed or null
            if (_dbConnection == null || _dbConnection.State != System.Data.ConnectionState.Open)
            {
                MySQLInit();
            }

            string query = "SELECT * FROM your_table_name";
            using (MySqlCommand cmd = new MySqlCommand(query, _dbConnection))
            {
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // Access your columns like reader["column_name"]
                        Console.WriteLine($"ID: {reader["id"]}, Name: {reader["name"]}");
                    }
                }
            }
        }

        // Clean up the connection when you're done with it
        public void CloseConnection()
        {
            if (_dbConnection != null && _dbConnection.State == System.Data.ConnectionState.Open)
            {
                _dbConnection.Close();
            }
        }
    }
}
3. Critical Setup Checks for Ubuntu/Mono
  • MySQL User Permissions: Make sure your MySQL user has access from localhost (since you're connecting locally). Run these commands in the MySQL shell to verify and grant access if needed:
    SELECT User, Host FROM mysql.user;
    -- If your user isn't listed for localhost, add it:
    GRANT ALL PRIVILEGES ON your_database.* TO 'your_db_user'@'localhost' IDENTIFIED BY 'your_db_pass';
    FLUSH PRIVILEGES;
    
  • Mono Version: Ensure you're running a recent Mono version (4.0 or newer) to avoid compatibility issues with the MySQL connector. Update Mono with:
    sudo apt-get update && sudo apt-get upgrade mono-complete
    
  • SSL Settings: If your MySQL server requires SSL, change SslMode=None in the connection string to SslMode=Required or SslMode=Preferred based on your server's config.
4. Why ADODB Isn't a Good Fit Here

ADODB depends on Windows COM interop, which Mono only partially supports. Even if you manage to get it working, you'll likely run into random crashes, performance issues, or missing support for newer MySQL features. The official connector is maintained by MySQL and built specifically for cross-platform .NET/Mono use, so it's the way to go.

内容的提问来源于stack exchange,提问作者user7431543

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:04:27