如何连接MySQL数据库?

问题描述 投票:0回答:6

C# 编程新手,我希望能够访问

MySQL
数据库。

我知道 C# 开发需要

MySQL connector/NET
MySQL for Visual Studio

我需要将它们安装到我的应用程序中吗?
我可以直接用程序释放连接器 DLL 吗?

更新:
最终用户需要两者还是仅连接器需要?
他们还需要什么吗?

c# mysql nuget-package mysql-connector
6个回答
121
投票

安装 Oracle 的

MySql.Data
NuGet 包

using MySql.Data;
using MySql.Data.MySqlClient;

namespace Data
{
    public class DBConnection
    {
        private DBConnection()
        {
        }

        public string Server { get; set; }
        public string DatabaseName { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        public MySqlConnection Connection { get; set;}

        private static DBConnection _instance = null;
        public static DBConnection Instance()
        {
            if (_instance == null)
                _instance = new DBConnection();
           return _instance;
        }
    
        public bool IsConnect()
        {
            if (Connection == null)
            {
                if (String.IsNullOrEmpty(databaseName))
                    return false;
                string connstring = string.Format("Server={0}; database={1}; UID={2}; password={3}", Server, DatabaseName, UserName, Password);
                Connection = new MySqlConnection(connstring);
                Connection.Open();
            }
    
            return true;
        }
    
        public void Close()
        {
            Connection.Close();
        }        
    }
}

示例:

var dbCon = DBConnection.Instance();
dbCon.Server = "YourServer";
dbCon.DatabaseName = "YourDatabase";
dbCon.UserName = "YourUsername";
dbCon.Password = "YourPassword";
if (dbCon.IsConnect())
{
    //suppose col0 and col1 are defined as VARCHAR in the DB
    string query = "SELECT col0,col1 FROM YourTable";
    var cmd = new MySqlCommand(query, dbCon.Connection);
    var reader = cmd.ExecuteReader();
    while(reader.Read())
    {
        string someStringFromColumnZero = reader.GetString(0);
        string someStringFromColumnOne = reader.GetString(1);
        Console.WriteLine(someStringFromColumnZero + "," + someStringFromColumnOne);
    }
    dbCon.Close();
}

56
投票

您可以使用包管理器将其添加为包,这是最简单的方法。您不需要任何其他东西来使用 MySQL 数据库。

或者您可以在程序包管理器控制台中运行此命令:

PM> Install-Package MySql.Data

NuGet MySql.Data


19
投票

您必须从这里下载MySQLConnection NET。

然后你需要将

MySql.Data.DLL
添加到 MSVisualStudio,如下所示:

  1. 打开菜单项目
  2. 添加
  3. 参考
  4. 浏览至
    C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.12\Assemblies\v4.5.2
  5. 添加MySql.Data.dll

如果您想了解更多信息,请访问:在此处输入链接描述

要在代码中使用,您必须导入库:

using MySql.Data.MySqlClient;

通过Click事件连接到Mysql数据库(无SSL模式)的示例:

using System;
using System.Windows;
using MySql.Data.MySqlClient;


namespace Deportes_WPF
{

public partial class Login : Window
{
    private MySqlConnection connection;
    private string server;
    private string database;
    private string user;
    private string password;
    private string port;
    private string connectionString;
    private string sslM;

    public Login()
    {
        InitializeComponent();

        server = "server_name";
        database = "database_name";
        user = "user_id";
        password = "password";
        port = "3306";
        sslM = "none";

        connectionString = String.Format("server={0};port={1};user id={2}; password={3}; database={4}; SslMode={5}", server, port, user, password, database, sslM);

        connection = new MySqlConnection(connectionString);
    }

    private void conexion()
    {
        try
        {
            connection.Open();

            MessageBox.Show("successful connection");

            connection.Close();
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message + connectionString);
        }
    }

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        conexion();
    }
  }

}

3
投票

看下面的代码,我尝试了一下,发现: 不要写

DBCon  = DBConnection.Instance();
你应该写
DBConnection DBCon - new DBConnection();
(这对我有用)

而不是

MySqlComman cmd = new MySqlComman(query, DBCon.GetConnection());
你应该放
MySqlCommand cmd = new MySqlCommand(query, DBCon.GetConnection());
(它缺少 d)


1
投票

另一个需要考虑的库是 MySqlConnector,https://mysqlconnector.net/。 Mysql.Data 采用 GPL 许可证,而 MySqlConnector 则采用 MIT。


-6
投票
 private void Initialize()
    {
        server = "localhost";
        database = "connectcsharptomysql";
        uid = "username";
        password = "password";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
        database + ";" + "U`enter code here`ID=" + uid + ";" + "PASSWORD=" + password + ";";

        connection = new MySqlConnection(connectionString);
    }
© www.soinside.com 2019 - 2024. All rights reserved.