一、Navicat配置
这个软件需要破解
密码是:123456;
新建连接=》新建数据库

创建一个表
保存出现名字设置

双击打开
把id设置为自动递增

这里就相当于每一次向数据库添加一个语句,会自动增长id一次

二、数据库的增删改查
1、Vs
建一个控制台的工程

2、引入Dll


三、数据库的增删改查
先看代码
cs
using MySql.Data.MySqlClient;
using System;
class Program
{
static MySqlConnection conn = null;
static void Main(string[] args)
{
//传入连接参数:数据库名称;用户Id、密码;表的名字;字符集
conn=new MySqlConnection("server=localhost;User Id=root;password=123456;Database=studymysql;Charset=utf8");
conn.Open();
//增
//Add();
删
Delete();
改
//Update();
查
//Query();
Console.ReadKey();
conn.Close();
}
static void Add()
{
MySqlCommand cmd = new MySqlCommand("insert into userinfo set name='xixi',age=96", conn);
cmd.ExecuteNonQuery();
int id = (int)cmd.LastInsertedId;
Console.WriteLine("Sql Insert Key:{0}:",id);
}
static void Delete()
{
MySqlCommand cmd = new MySqlCommand("delete from userinfo where id=1", conn);
cmd.ExecuteNonQuery();
Console.WriteLine("delete done");
}
static void Update()
{
MySqlCommand cmd = new MySqlCommand("update userinfo set name=@name,age=@age where id=@id", conn);
cmd.Parameters.AddWithValue("name", "xoxo");
cmd.Parameters.AddWithValue("age", 123);
cmd.Parameters.AddWithValue("id", 2);
cmd.ExecuteNonQuery();
Console.WriteLine("update done");
}
static void Query()
{
MySqlCommand cmd = new MySqlCommand("select *from userinfo where name='xixi'",conn);
MySqlDataReader reader=cmd.ExecuteReader();
while (reader.Read())
{
int id=reader.GetInt32("id");
string name=reader.GetString("name");
int age = reader.GetInt32("age");
Console.WriteLine(string.Format("sql result:id:{0} name:{1} age:{2}", id, name, age));
}
}
}
1、引入数据库dll

2、传入连接参数
新建一个连接在主方法中,传入连接参数
并调用增删改查的方法
注:要在连接时开启,使用后连接关闭
3、增
传入增加的信息(数据库语言)
ExecuteNonQuery
方法用于执行SQL语句,如插入、更新或删除操作,这些操作不返回数据。
把最后一个id赋值给id
输出

4、删
写一个sql语句,删除id为1的数据
5、改
注:这么写可以防止SQL注入


6、查
使用一个while循环而不是if循环,原因是查找时只会找到第一个满足条件的后,不再进行查找
