无法在 C# 中连接到 mySQL [英] Can't connect to mySQL in C#

查看:104
本文介绍了无法在 C# 中连接到 mySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 c# 应用程序中连接 mySQL 数据库,但我遇到了一个无法正常工作的非常短的代码.

I'm trying to connect mySQL database in my c# application but I'm stuck with a very short code that doesn't work.

我尝试了很多不同的代码,但都失败了.我还尝试使用远程 phpmyadmin 数据库和 localhost 数据库,但都不起作用.

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 服务器上禁用了防火墙.我已经在我的电脑上打开了我的 3306 端口.

I'm connected to both of them and copy-pasted their logins in my code. I've disabled firewalls on my phpmyadmin server. I've open my 3306 port on my PC.

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();
        }
    }
}

它应该弹出一个带有连接打开!"的消息框.但它实际上加载了 30 秒并给了我一条错误消息:找不到或无法访问服务器".

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".

你有什么想法吗?我的代码很短,我所做的所有搜索都与我得到的代码相似.

Do you have any idea? My code is very short and all the searches I did where similar to the code I got there.

推荐答案

首先,您需要下载适用于 .NET 的 MySql 数据连接器.您可以在 https://dev.mysql.com/downloads/connector/net 上找到它/.接下来,安装后,需要在项目中添加对MySql库的引用.请参阅此处如何操作

First you need to download the MySql data connector for .NET. You can find it here at https://dev.mysql.com/downloads/connector/net/. Next, after installing it, you need to add a reference to the MySql library to your project. See here how to do it

或者您可以简单地使用 NuGet 包管理器自动下载和安装连接器.

Or you can simply use the NuGet Package Manager to download and install the connector automatically.

无论如何,在正确安装并引用库之后,您应该将 using MySql.Data.MySqlClient; 行添加到您的 cs 文件中,现在您已准备好使用这些类需要连接到 MySql 并处理其数据.

In any case, after the correct installation and referencing the library, you should add, to your cs file, the using MySql.Data.MySqlClient; line and now you are ready to use the classes required to connect to MySql and work with its data.

所以你的代码应该是

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.

这篇关于无法在 C# 中连接到 mySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆