将MySQL连接到Visual Studio C# [英] Connecting MySQL to Visual Studio C#

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

问题描述

我正在尝试编写数据访问代码以将MySQL连接到Visual Studio。到目前为止,我已经有了此代码,但是我不知道它是否正确。我正在使用我的书,并已注释掉其他一些内容,但是在包含注释的信息时出现错误。

I'm trying to write data access code to connect MySQL to visual studio. I have this code so far, but I do not know if it's correct. I'm using my books and have commented out a few other things, but I received errors when I included the commented out information.

public static MySqlConnection getconnection()

{
string connectionstring = "serve=r127.0.0.1; uid = root;" + "pwd = databasses; database = group1;"

MySqlConnection connection = new MySqlConnection(connectionstring);
return connection;
}

注释信息:

//string connectionstring = "Data Source=localhost\\MySql;Initial Catalog = group1; Integrated Security=True";

and

//conection.Open();

我是否可以检查是否建立了连接?我可以运行它并显示一个空白表格,但是我担心这是错误的。

Is there a way for me to check if my connection is made? I am able to run it and display a blank form, but I'm worried it's wrong.

推荐答案

从MySQL 网站以下是他们有关如何使用C#连接到MySQL数据库的示例代码:

From the MySQL website here is their example code on how to use C# to connect to a MySQL database:

MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;

myConnectionString = "server=127.0.0.1;uid=root;" +
    "pwd=12345;database=test;";

try
{
    conn = new MySql.Data.MySqlClient.MySqlConnection();
    conn.ConnectionString = myConnectionString;
    conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    MessageBox.Show(ex.Message);
}

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

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