简体   繁体   English

无法在 C# 中连接到 mySQL

[英]Can't connect to mySQL in C#

I'm trying to connect mySQL database in my c# application but I'm stuck with a very short code that doesn't work.我正在尝试在我的 c# 应用程序中连接 mySQL 数据库,但我遇到了一个不起作用的非常短的代码。

I've tried a bunch of different code but all of them failed.我尝试了一堆不同的代码,但都失败了。 I also tried to use a remote phpmyadmin database and a localhost database, none of them worked.我还尝试使用远程 phpmyadmin 数据库和 localhost 数据库,它们都不起作用。

I'm connected to both of them and copy-pasted their logins in my code.我已连接到他们两个并将他们的登录名复制粘贴到我的代码中。 I've disabled firewalls on my phpmyadmin server.我在我的 phpmyadmin 服务器上禁用了防火墙。 I've open my 3306 port on my PC.我已经在我的 PC 上打开了我的 3306 端口。

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Tests
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            string connetionString;
            SqlConnection cnn;
            connetionString = @"Data Source=localhost;Initial Catalog=my_projects;User ID=root;Password=123456";
            cnn = new SqlConnection(connetionString);
            cnn.Open();
            MessageBox.Show("Connection Open  !");
            cnn.Close();
        }
    }
}

It should pop a message box with "Connection Open !"它应该弹出一个带有“连接打开!”的消息框。 but it actualy loads for 30 seconds and gives me an error message: "The server can not be found or is not accessible".但它实际上加载了 30 秒并给了我一条错误消息:“找不到服务器或无法访问服务器”。

Do you have any idea?你有什么主意吗? My code is very short and all the searches I did where similar to the code I got there.我的代码很短,我所做的所有搜索都与我得到的代码相似。

First you need to download the MySql data connector for .NET.首先,您需要下载适用于 .NET 的 MySql 数据连接器。 You can find it here at https://dev.mysql.com/downloads/connector/net/ .您可以在https://dev.mysql.com/downloads/connector/net/找到它。 Next, after installing it, you need to add a reference to the MySql library to your project.接下来,安装后,需要在项目中添加对MySql库的引用。 See here how to do it这里怎么做

Or you can simply use the NuGet Package Manager to download and install the connector automatically.或者,您可以简单地使用 NuGet 包管理器来自动下载和安装连接器。

In any case, after the correct installation and referencing the library, you should add, to your cs file, the using MySql.Data.MySqlClient;无论如何,在正确安装并引用库之后,您应该在您的 cs 文件中添加using MySql.Data.MySqlClient; line and now you are ready to use the classes required to connect to MySql and work with its data.行,现在您已准备好使用连接到 MySql 并处理其数据所需的类。

So your code should be所以你的代码应该是

using MySql.Data.MySqlClient;

... other code ....

private void Button1_Click(object sender, EventArgs e)
{

    try
    {
        string connetionString = @"Server=localhost;Database=my_projects;User ID=root;Password=123456";
        using(MySqlConnection cnn = new MySqlConnection(connetionString))
        {
            cnn.Open();
            MessageBox.Show("Connection Open  !");
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show("Cannot open connection: Reason:" + ex.Message);
    }

}

Remember that the connection to your database contains unmanaged resources and you should always add the using statement around these kind of objects.请记住,与数据库的连接包含非托管资源,您应该始终围绕这些类型的对象添加using 语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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