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:
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:
If you're using a project file (like .csproj), you can also add thesudo apt-get install libmysql6.1-cilMySql.Datapackage via NuGet for easier dependency management.
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(); } } } }
- 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=Nonein the connection string toSslMode=RequiredorSslMode=Preferredbased on your server's config.
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

