vs2010C#连接MySQL

参考:https://www.cnblogs.com/haiyishui/articles/3783103.html

在mysql官网下载连接器, 这个是地址 http://dev.mysql.com/downloads/connector/net/

 
下载完成后将bin目录里面的MySql.Data.dll添加到工程的引用中.确保引用中有mysql.data.
 
然后是在mysql中创建mysqltest数据库,然后用下面的代码创建表
 
using System;
using MySql.Data;
using MySql.Data.MySqlClient;
  
 
1.连接数据库
  
//string StrCon = "Database=basicsthree;Data Source = 114.116.148.63;User Id=root;password=123456789;charset=utf8;port=3306";  //服务器数据库连接字符串
            string Strcon = "Database=c;Data Source =localhost;User id=root;password=root;charset=utf8;port=3306";  //本地数据库连接字符串

            MySqlConnection conn = new MySqlConnection(Strcon);
            conn.Open();

2.命令控制

string sSQL = null;
            sSQL = "select * from tb_users";
            MySqlCommand cmd = new MySqlCommand(sSQL, conn);

3.返回查询到的内容

 MySqlDataReader reader = cmd.ExecuteReader();
            string sCnt = null;
            while (reader.Read())
            {
                sCnt = reader.GetString(0);
            }
            reader.Close();
            conn.Close();
            textBoxAddress.Text = sCnt;

 

posted @ 2018-11-18 19:15  StarZhai  阅读(3172)  评论(0编辑  收藏  举报