dapper的使用
安装
控制台中安装
Install-Package Dapper
方法 :Execute
返回: 受影响的行数
执行: 删 改 查
参数:
Execute(sql, //查询语句
param, // default = null 参数
transaction, //default = null 事务
commandTimeout, //default = null 最大连接时间
commandType //default = null 命令类型?
)
代码:
/// <summary>
/// Execute插入一条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
Person person = new Person("zhangsan","12345234@gmail.com","3212151343","男");
string sqlstr = "INSERT INTO person(name,email,gender,phone) VALUES(@name,@email,@gender,@phone) ";
int affectedRows = conn.Execute(sqlstr,
person,
null,
null);
textBox1.Text = affectedRows.ToString();
}
}
/// <summary>
/// Execute插入多条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
List<Person> personList = new List<Person>();
personList.Add(new Person("zhangsan", "12345234@gmail.com", "3212151343", "男"));
personList.Add(new Person("zhsffa", "1232431534@gmail.com", "31511511543", "男"));
string sqlstr = "INSERT INTO person(name,email,gender,phone) VALUES(@name,@email,@gender,@phone) ";
int affectedRows = conn.Execute(sqlstr,
personList,
null,
null);
textBox1.Text = affectedRows.ToString();
}
}
/// <summary>
/// Execute 更新一条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
Person person = new Person();
person.Id = 15;
person.Gender = "改改看";
string sqlstr = "UPDATE person SET gender = @gender WHERE id = @id ";
int affectedRows = conn.Execute(sqlstr,
person,
null,
null);
textBox1.Text = affectedRows.ToString();
}
}
/// <summary>
/// Execute 更新多条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
List<Person> personList = new List<Person>();
personList.Add(new Person(3,"", "", "3212151343", "^-^"));
personList.Add(new Person(5,"zhsffa", "1232431534@gmail.com", "31511511543", "^0^"));
string sqlstr = "UPDATE person SET gender = @gender WHERE id = @id ";
int affectedRows = conn.Execute(sqlstr,
personList,
null,
null);
textBox1.Text = affectedRows.ToString();
}
}
/// <summary>
/// Execute 删除一条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
string sqlstr = "DELETE FROM person WHERE id = @id ";
int affectedRows = conn.Execute(sqlstr,
new { id = 9},
null,
null);
textBox1.Text = affectedRows.ToString();
}
}
方法 :Query
Query QueryFirst QueryFirstOrDefault
返回:映射结果
执行: 匿名 强类型? 一对一 一对多 多类型?
参数:
Query(sql, //查询语句
param, // default = null 参数
transaction, //default = null 事务
buffered, //default = null 查询的缓冲结果?
commandTimeout, //default = null 最大连接时间
commandType //default = null 命令类型?
)
代码
/// <summary>
/// Query 查询一条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
string sqlstr = "SELECT * FROM person ";
Person person = conn.Query<Person>(sqlstr).FirstOrDefault();
textBox1.Text = person.ToString();
}
}
/// <summary>
/// 查询多条
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
List<Person> personList = new List<Person>();
string sqlstr = "SELECT * FROM person ";
personList = conn.Query<Person>(sqlstr).ToList();
string str = null;
foreach (Person person in personList)
{
str += person.ToString();
}
textBox1.Text = str;
}
}
/// <summary>
/// Query 一对一查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_Click(object sender, EventArgs e)
{
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
List<Person> personList = new List<Person>();
string sqlstr = "SELECT p.*,r.role_name FROM person p INNER JOIN role r ON p.role_id = r.role_id";
var list = conn.Query<Person, Role, Person>(sqlstr,
(person, role) => { person.Role = role; return person; },
null,
null,
true,
splitOn:"role_id"
).ToList();
this.dataGridView1.DataSource = list;
}
}
/// <summary>
/// Query 一对多查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button10_Click(object sender, EventArgs e)
{
// 姑且这么用,但是很多东西还不明白 比如赋值的过程
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
List<Person> personList = new List<Person>();
var lookUp = new Dictionary<int, Person>();
string sqlstr = "SELECT p.* ,r.*,s.* " +
"from person_role s " +
"left join person p on p.id = s.person_id " +
"left join role r on s.role_id = r.role_id";
var list = conn.Query<Person, Role, Person>(sqlstr,
(person, role) => {
Person p;
if (!lookUp.TryGetValue(person.Id,out p))
{
lookUp.Add(person.Id, p = person);
}
person.RoleList.Add(role);
return person; },
null,
null,
true,
splitOn:"role_id,id"
).ToList();
this.dataGridView1.DataSource = list;
}
}
事务
/// <summary>
/// 事务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button11_Click(object sender, EventArgs e)
{
using (IDbConnection conn = SqlConnection.MySqlConnection())
{
IDbTransaction trans = conn.BeginTransaction();
try
{
Person person1 = new Person("zhangsan", "12345234@gmail.com", "3212151343", "男");
Person person2 = new Person("asdfban", "2345765@gmail.com", "87654", "男");
string sqlstr = "INSERT INTO person(name,email,gender,phone) VALUES(@name,@email,@gender,@phone) ";
int affectedRows1 = conn.Execute(sqlstr,
person1,
trans,
null);
int affectedRows2 = conn.Execute(sqlstr,
person2,
trans,
null);
trans.Commit();
textBox1.Text = affectedRows1.ToString() + "---"+ affectedRows2.ToString();
}
catch (Exception)
{
trans.Rollback();
}
}
}