参考: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;