简体   繁体   中英

C#.Net MySQL Database Connection on Ubuntu and Win10

I made a C# console app in visual studio to run as a TCP server using .Net Sockets. I can build this and run it on my Ubuntu (digital ocean droplet) using mono and it works well. However, this C# program also uses a MySQL database on my localhost which works like this:

using System;
using ADODB;

namespace BlackjackServer
{
class MySQL
{
    public Recordset DB_RS;
    public Connection DB_CONN;

    public void MySQLInit()
    {
        try
        {
            DB_RS = new Recordset();
            DB_CONN = new Connection();

            DB_CONN.ConnectionString = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Port=3306;Database=blackjack;User=root;Password=;Option=3;";
            DB_CONN.CursorLocation = CursorLocationEnum.adUseServer;
            DB_CONN.Open();
            Console.WriteLine("Connection to DB was successful");

            //For Testing
            var db = DB_RS;
            {
                db.Open("SELECT * FROM users WHERE 0=1", DB_CONN, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic);

                db.AddNew();
                db.Fields["username"].Value = "test";
                db.Fields["password"].Value = "test";
                db.Fields["email"].Value = "test@test.com";
                db.Update();

                db.Close();
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine("Exception: " + ex);
            Console.ReadLine();
        }
    }
}
}

On my localhost this works but I need to modify it to also work on Ubuntu. I already have the MySQL database setup on ubuntu and I tried getting some unixODBC drivers but can't seem to find how to use them to connect through this program I wrote. Any suggestions?

I have found a method that works (for now) on both my Win10 environment for testing and my Ubuntu server for deployment. This is MySQL.Data.MySqlClient (Connector/Net Driver) which worked out of the box with the driver I had installed on the Ubuntu server already.

I followed this tutorial to get set up: http://zetcode.com/db/mysqlcsharptutorial/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM