C#连接mysql数据库

下载驱动安装:mysql-connector-net-6.7.4.msi

从visual studio 2010 中“添加引用”将C:\Program Files (x86)\MySQL\MySQL Connector Net 6.7.4\Assemblies\v4.5\MySql.Data.dll加入,再在类中加入using Mysql.Data.MySqlClient

using System;
using MySql.Data.MySqlClient;

class DBConnection
{
     public void connection ()
     {
         //定义连接字符串
         string url = "server=127.0.0.1;user=root;database=cwgl;password=123456;";
         //定义连接对象sConn
         MySqlConnection sConn = new MySqlConnection(url);
         try{
              //打开连接
              sConn.Open();
              Console.WriteLine("连接成功");
         }
         catch(Exception ex)
         {       //给出错误信息
              Console.WriteLine("连接错误:"+ex.Message);
         }   
         /**查询数据**/
         /*
         string sta = "select * from cwgl_user";//执行一个简单的语句
         MySqlCommand comm = new MySqlCommand(sta, sConn);
         MySqlDataReader reader = comm.ExecuteReader();//用MySqlDataReader接收执行结果
         while (reader.Read())
         {
             Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1) + " " + reader.GetString(2) );//读出查询的结果
         }
         reader.Close();
         /**查询数据**/
         string sta = "insert into cwgl_user(user_name,user_password) values('liuyingjie','123456')";
         //string sta = "delete from cwgl_user where user_name='刘英杰'";
         MySqlCommand comm = new MySqlCommand(sta,sConn);
         comm.ExecuteNonQuery();
         //关闭数据库连接
         sConn.Close();

     }
}


 

以下是C#连接MySQL数据库的步骤和示例代码: 1. 首先需要安装MySQL Connector/NET,可以在MySQL官网下载安装包进行安装。 2. 在C#项目中添加对MySQL Connector/NET的引用。 3. 在代码中使用以下代码进行连接: ```csharp using MySql.Data.MySqlClient; string connStr = "server=127.0.0.1;port=3306;user=root;password=power123;database=my_data;"; MySqlConnection conn = new MySqlConnection(connStr); try { conn.Open(); Console.WriteLine("MySQL连接成功!"); } catch (MySqlException ex) { Console.WriteLine("MySQL连接失败:" + ex.Message);} finally { conn.Close(); } ``` 其中,`connStr`是连接字符串,包含了MySQL服务器的IP地址、端口号、用户名、密码和数据库名。`MySqlConnection`是MySQL Connector/NET提供的连接对象,通过`Open()`方法打开连接,`Close()`方法关闭连接。 4. 连接成功后,可以使用`MySqlCommand`对象执行SQL语句,例如: ```csharp string sql = "SELECT * FROM my_table"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader.GetString(0) + "\t" + reader.GetString(1)); } reader.Close(); ``` 其中,`sql`是要执行的SQL语句,`MySqlCommand`是MySQL Connector/NET提供的执行对象,通过`ExecuteReader()`方法执行SQL语句并返回一个`MySqlDataReader`对象,通过`Read()`方法读取查询结果。 5. 可以使用`DataGridView`控件显示查询结果,例如: ```csharp string sql = "SELECT * FROM my_table";MySqlDataAdapter adapter = new MySqlDataAdapter(sql, conn); DataSet ds = new DataSet(); adapter.Fill(ds, "my_table"); dataGridView1.DataSource = ds.Tables["my_table"]; ``` 其中,`MySqlDataAdapter`是MySQL Connector/NET提供的数据适配器对象,通过`Fill()`方法将查询结果填充到`DataSet`对象中,然后将`DataSet`对象中的表绑定到`DataGridView`控件上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值