如何在C#中连接MySQL数据库? [英] How to connect MySQL database in C#?

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

问题描述

我发现在C#中连接数据库很困难?你能给我一些技巧吗?

I find difficult in connecting the database in C#? Can you give me some techniques?

推荐答案

MySQL的ADO.net提供程序:http://dev.mysql.com/downloads/connector/net [ ^ ]



然后添加一些:通过ADO.NET接口简化数据库访问 [ ^ ]
The ADO.net provider for MySQL: http://dev.mysql.com/downloads/connector/net[^]

And then add some: Simplified Database Access via ADO.NET Interfaces[^]






您可以实现以下方式连接mySQL数据库。



1)在页面中添加名称空间

i)使用MySql.Data;

ii)使用MySql.Data.MySqlClient;



2)获取连接字符串或创建格式



Str ing ConnectionString =server = 127.0.0.1; uid = test; pwd = test @ 123; database = test;;



3)在你所在的班级想要连接数据库写下面的代码行,



公共类测试

{



// appilcation逻辑到这里



试试

{



//应用程序逻辑在这里



MySqlConnection Myconn = new MySqlConnection();

Myconn.ConnectionString = ConnectionString;

Myconn.Open();

}

catch(MySqlException ex)

{

MessageBox.Show(ex.Message);

}



终于

{

if(Myconn.State == ConnectionState.Open)

{

Myconn.Close();

}



}



}





//如果您想传递一些命令,请按照以下步骤进行操作



//在MyConn.Open()语句中打开连接后创建如下命令。



MySqlCommand cmd = MyConn.CreateCommand();

cmd.CommandText =Select * FROM Employee;

MySqlDataAdapter adpt = new MySqlDataAdapter(cmd);

Dataset ds = new Dataset() ;

adpt.Fill(ds);

//找到任何控件或相应操作的数据集



希望你现在理解这个概念..
Hi,

You can implement the below way to connect with mySQL db.

1) Add namespaces in your page
i)using MySql.Data;
ii)using MySql.Data.MySqlClient;

2) get connection string or create in format

String ConnectionString= "server=127.0.0.1;uid=test;pwd=test@123;database=test;";

3) In the class from where you want to connect to database write below Line of code,

Public Class Test
{

// appilcation logic goes here

try
{

// Application logic goes here

MySqlConnection Myconn = new MySqlConnection();
Myconn.ConnectionString = ConnectionString;
Myconn.Open();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}

finally
{
if(Myconn.State==ConnectionState.Open)
{
Myconn.Close();
}

}

}


// if you want to pass some command then follow below steps

// create command as below after opening the connection in MyConn.Open()statement.

MySqlCommand cmd=MyConn.CreateCommand();
cmd.CommandText="Select * FROM Employee";
MySqlDataAdapter adpt=new MySqlDataAdapter(cmd);
Dataset ds=new Dataset();
adpt.Fill(ds);
//find the dataset with any control or manipulate accordingly

Hope you understand the concept now..


首先你需要连接器/网络

http://dev.mysql.com/downloads/connector/net/ [ ^ ]



此外你应该阅读 MySQL Connector / Net开发人员指南

http:// dev.mysql.com/doc/connector-net/en/index.html [ ^ ]
First of all you need Connector/Net
http://dev.mysql.com/downloads/connector/net/[^]

Moreover you should read MySQL Connector/Net Developer Guide
http://dev.mysql.com/doc/connector-net/en/index.html[^]


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

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