mysql下载链接Winform实现链接Mysql数据库
⼀、Mysql.Data.dll
连接数据库、操作数据库,本质是利⽤数据库提供的动态链接库MySql.Data.dll进⾏操作。MySql.Data.dll提供以下8个类:MySqlConnection: 连接MySQL服务器数据库。
MySqlCommand:执⾏⼀条sql语句。
MySqlDataReader: 包含sql语句执⾏的结果,并提供⼀个⽅法从结果中阅读⼀⾏。
MySqlTransaction: 代表⼀个SQL事务在⼀个MySQL数据库。
MySqlException: MySQL报错时返回的Exception。
MySqlCommandBuilder: Automatically generates single-table commands used to reconcile changes made to a DataSet with the associated MySQL database.
MySqlDataAdapter: Represents a set of data commands and a database connection that are used to fill a data set and update a MySQL database.
MySqlHelper: Helper class that makes it easier to work with the provider.
⼆、实例操作(操作环境:Visual Studio 2019)
(1)创建Winform程序,安装Mysql.Data
①创建项⽬后,在解决⽅案资源管理器中右键引⽤-点击管理NuGet包
②在浏览中搜索 mysql,下载Mysql.Data
按提⽰步骤安装完成即可。
(2)建⽴连接
①创建Winform窗体
②实现代码
using MySql.Data.MySqlClient;    //务必引⼊MySql.Data.MySqlClient
//按钮单击事件
private void button1_Click(object sender, EventArgs e)
{
/** server = 127.0.0.1或者localhost 代表本机地址; port = 3306 端⼝号; **/            /** user ⽤户名; password 密码; database 数据库名称; **/
string connstr = "server = 127.0.0.1; port = 3306; user = root ; password = 123456; database = winformtest";
MySqlConnection conn = new MySqlConnection(connstr);
try
{
//可能出现异常
conn.Open();
MessageBox.Show("链接成功!");
}
catch(MySqlException ex)
{
//异常则提⽰异常信息
MessageBox.Show(ex.Message);
}
finally
{
//务必关闭MysqlConnection
conn.Close();
}
}
③测试结果
(3)增删改查
①查询
1)查询条件固定
string sql= "select * from user";
MySqlCommand cmd = new MySqlCommand(sql,conn);
MySqlDataReader reader =cmd.ExecuteReader();//执⾏ExecuteReader()返回⼀个MySqlDataReader对象
while (reader.Read())//初始索引是-1,执⾏读取下⼀⾏数据,返回值是bool
{
//Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString());
//Console.WriteLine(reader.GetInt32(0)+reader.GetString(1)+reader.GetString(2));
Console.WriteLine(reader.GetInt32("userid") + reader.GetString("username") + reader.GetString("password"));//"userid"是数据库对应的列名,推荐这种⽅式
}
2)查询条件不固定
//string sql = "select * from user where username='"+username+"' and password='"+password+"'"; //我们⾃⼰按照查询条件去组拼
string sql = "select * from user where username=@para1 and password=@para2";//在sql语句中定义parameter,然后再给parameter赋值
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("para1", username);
cmd.Parameters.AddWithValue("para2", password);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())//如果⽤户名和密码正确则能查询到⼀条语句,即读取下⼀⾏返回true
{
return true;
}
②增减、删除、修改
string sql = "insert into user(username,password,registerdate) values('啊宽','123','"+DateTime.Now+"')";
//string sql = "delete from user where userid='9'";
//string sql = "update user set username='啊哈',password='123' where userid='8'";
MySqlCommand cmd = new MySqlCommand(sql,conn);
int result =cmd.ExecuteNonQuery();//3.执⾏插⼊、删除、更改语句。执⾏成功返回受影响的数据的⾏数,返回1可做true判断。执⾏失败不返回任何数据,报错,下⾯
代码参考博⽂:
(4)总结
C#(Winform)链接Mysql步骤:
①链接数据库
②确认并创建SQL语句
③执⾏SQL语句
④对SQL语句返回的结果进⾏处理
⑤关闭链接(切记!)
/**
*①链接数据库
*②确认并创建SQL语句
*③执⾏SQL语句
*④对SQL语句返回的结果进⾏处理
*⑤关闭链接(切记!)
**/
/** ①链接数据库:导⼊dll、定义链接语句coonetstr、创建MysqlConnection、打开conn(异常处理) **/
using MySql.Data.MySqlClient;
String connetStr = "server=127.0.0.1;port=3306;user=root;password=123456; database=winformtest;Allow User Variables=True"; MySqlConnection conn = new MySqlConnection(connetStr);
try{
conn.Open();
/** ②确认并创建SQL语句:创建所需的sql语句 **/
string sql = "select * from user where name = @para1 and password = @para2";
//string sql = "insert into user value(@para1,@para2);
//string sql = "update user set password = @para1 where name = @para2 and password = @para3";
//string sql = "delete from user where name = @para1 and password = @para2";
MySqlCommand cmd = new MySqlCommand(sql, conn);
/** ③执⾏SQL语句 && ④对SQL语句返回的结果进⾏处理**/
/** Select **/
cmd.Parameters.AddWithValue("para1", textBox1.Text);
cmd.Parameters.AddWithValue("para2", textBox2.Text);
MySqlDataReader reader = cmd.ExecuteReader();    //增删改和查询的区别
if (reader.Read())
{
MessageBox.Show("登录成功!");
}
/** Insert、Update、Delete **/
/**
cmd.Parameters.AddWithValue("para1", textBox1.Text);
cmd.Parameters.AddWithValue("para2", textBox2.Text);
int reader = cmd.ExecuteNonQuery();        //增删改和查询的区别
if (reader.Read())
{
MessageBox.Show("成功!");
}
**/
}
catch(MySqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
/** ⑤关闭链接(切记!) **/
conn.Close();
}
附C#课程作业实例(Winform + Mysql 实现的停车场收费管理系统):                              GitHub :
CSDN:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。