在此之前先下载一个mysql.dll 库 如下
链接:https://pan.baidu.com/s/17Nv_1W3KbXfWgdOvWf_Stg
提取码:hi3w
难度 : ⭐⭐⭐(全星5颗星的情况下)
在工程 添加引用,直接浏览到那个文件即可,选中那个dll库
这个增删改查可以抽离成一个SqlHelper 类,直接新建一个类库,选中Framwork 框架下的,然后在其他工程添加引用即可;
数据库建表语句
/*
SQLyog Ultimate v10.00 Beta1
MySQL - 5.7.18-log
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
create table `tt_user` (
`id` int ,
`name` varchar ,
`age` int ,
`address` varchar
);
insert into `tt_user` (`id`, `name`, `age`, `address`) values('1','大军','26','上海市');
insert into `tt_user` (`id`, `name`, `age`, `address`) values('2','小杨','24','云南');
insert into `tt_user` (`id`, `name`, `age`, `address`) values('3','赵刘','27','陕西');
insert into `tt_user` (`id`, `name`, `age`, `address`) values('4','李四','23','安徽');
insert into `tt_user` (`id`, `name`, `age`, `address`) values('5','老刘','30','安徽安庆');
insert into `tt_user` (`id`, `name`, `age`, `address`) values('6','王二','22','北京市');
insert into `tt_user` (`id`, `name`, `age`, `address`) values('7','123','213','213');
增删改查核心代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace ASPNetSql
{
class Program
{
static void Main(string[] args)
{
// AddUser();
// UpdateUser();
QueryData();
// DeleteUser();
// CalcCount();
}
static void CalcCount()
{
String connetStr = "server=localhost;port=3306;user=root;password=root; database=test;";
// server=127.0.0.1/localhost 代表本机,端口号port默认是3306可以不写
MySqlConnection conn = new MySqlConnection(connetStr);
try
{
conn.Open();//开启数据库
Console.WriteLine("已连接。。");
string sql = "select count(*) from tt_user";
MySqlCommand cmd = new MySqlCommand(sql, conn);
object o = cmd.ExecuteScalar();//返回值即为查询结果
int count = Convert.ToInt32(o.ToString());
Console.WriteLine(count);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
conn.Close();//关闭连接
Console.ReadKey();
}
static void DeleteUser()
{
String connetStr = "server=localhost;port=3306;user=root;password=root; database=test;";
// server=127.0.0.1/localhost 代表本机,端口号port默认是3306可以不写
MySqlConnection conn = new MySqlConnection(connetStr);
try
{
conn.Open();//开启数据库
Console.WriteLine("已连接。。");
string sql = "delete from tt_user where id=10";
MySqlCommand cmd = new MySqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery();//返回值受数据库中数据的行数影响
Console.WriteLine("删除完成");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
conn.Close();//关闭连接
Console.ReadKey();
}
static void UpdateUser()
{
String connetStr = "server=localhost;port=3306;user=root;password=root; database=test;";
// server=127.0.0.1/localhost 代表本机,端口号port默认是3306可以不写
MySqlConnection conn = new MySqlConnection(connetStr);
try
{
conn.Open();//开启数据库
Console.WriteLine("已连接。。");
string sql = "update tt_user set name='军',address='上海市123' where id=8";
MySqlCommand cmd = new MySqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery();//返回值受数据库中数据的行数影响
Console.WriteLine("更新完成");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
conn.Close();//关闭连接
Console.ReadKey();
}
static void AddUser()
{
String connetStr = "server=localhost;port=3306;user=root;password=root; database=test;";
// server=127.0.0.1/localhost 代表本机,端口号port默认是3306可以不写
MySqlConnection conn = new MySqlConnection(connetStr);
try
{
conn.Open();//开启数据库
Console.WriteLine("已连接。。");
string sql = "insert into tt_user(name,age,address) values('君','27','安徽省')";//当前时间
MySqlCommand cmd = new MySqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery();//返回值受数据库中数据的行数影响
Console.WriteLine("插入完成");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
conn.Close();//关闭连接
Console.ReadKey();
}
static void QueryData()
{
String connetStr = "server=localhost;port=3306;user=root;password=root; database=test;";
// server=127.0.0.1/localhost 代表本机,端口号port默认是3306可以不写
MySqlConnection conn = new MySqlConnection(connetStr);
try
{
conn.Open();//建立连接,可能出现异常,使用try catch语句
Console.WriteLine("已经建立连接");
//在这里可以使用代码对数据库进行增删查改的操作
string sql = "select * from tt_user";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader reader = cmd.ExecuteReader();//查询
while (reader.Read())//Read()返回值bool
{
//Console.WriteLine(reader[0].ToString() + " " + reader[1].ToString() + " " + reader[2].ToString());//通过read数组取值
//Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1) + " " + reader.GetInt16(2));//通过get方法读取
Console.WriteLine(reader.GetInt16("id") + " " + reader.GetString("name") + " " + reader.GetInt16("age") + " " + reader.GetString("address"));//通过列名索引读取
}
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);//有错则报出错误
}
finally
{
conn.Close();//关闭通道
}
Console.ReadLine();
}
}
}