连接数据库、操作数据库,本质是利用数据库提供的动态链接库MySql.Data.dll进行操作。MySql.Data.dll提供以下8个类:
①创建项目后,在解决方案资源管理器中右键引用-点击管理NuGet包
②在浏览中搜索 mysql,下载Mysql.Data
按提示步骤安装完成即可。
①创建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();
}
}
③测试结果
①查询
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判断。执行失败不返回任何数据,报错,下面代码都不执行
代码参考博文:https://www.cnblogs.com/DonYao/p/8438375.html
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 :https://github.com/KittenStone/WinformFinalWork
CSDN:https://download.csdn.net/download/weixin_44720249/12480773