如何将MySQL数据库连接到C#WinForm应用程序? [英] How to connect MySQL database to C# WinForm Application?

查看:522
本文介绍了如何将MySQL数据库连接到C#WinForm应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将MySQL数据库连接到C#WinForm应用程序?

How do you connect a MySQL database to a C# WinForm Application?

我可以使用Microsoft SQL Server建立连接,但是我一生都无法弄清楚如何使用MySQL完成连接.

I can establish a connection using a Microsoft SQL Server, but cannot for the life of me figured out how it's done using MySQL.

推荐答案

使用以下代码:

string myConnectionString = "server=localhost;database=testDB;uid=root;pwd=abc123;";
private void button1_Click(object sender, EventArgs e)
{
        string connetionString = null;
        MySqlConnection cnn ;
        connetionString = "server=localhost;database=testDB;uid=root;pwd=abc123;";
        cnn = new MySqlConnection(connetionString);
        try
        {
            cnn.Open();
            MessageBox.Show ("Connection Open ! ");
            cnn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection ! ");
        }
}

请确保您的代码中有正确的引用:

Make sure that you have a proper reference in your code:

using MySql.Data.MySqlClient;

这个连接字符串只是一个例子.您必须查看什么是连接字符串ofc.还要搜索这类问题,因为我确信StackOverflow上会有很多类似甚至相同的问题.

And this connection string is just an example. You have to see what is your connection string ofc. And also search for these kind of questions because I'm sure that there is a bunch of similar or even the same questions on StackOverflow.

这篇关于如何将MySQL数据库连接到C#WinForm应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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