直接上代码,有空会上传Demo
using DapperTest.Models;
using System.Collections.Generic;
using System.Web.Http;
using Dapper;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Configuration;
namespace DapperTest.Controllers
{
public class HomeController : ApiController
{
#region 查询
///
/// 查询所有数据
///
///
[HttpGet]
public IHttpActionResult GetStudentList()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"SELECT * FROM STUDENT";
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Query(sql).ToList();
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
///
/// 查询指定ID单条数据(带参数)
///
///
[HttpGet]
public IHttpActionResult GetStudentInfo(string ID)
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"SELECT * FROM STUDENT WHERE STUID in @STUID";
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Query(sql, new { STUID = ID });
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
///
/// IN查询
///
///
[HttpGet]
public IHttpActionResult GetStudentInfos(string IDStr)
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"SELECT * FROM STUDENT WHERE STUID in @STUIDStr";
var IDArr = IDStr.Split(',');
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Query(sql, new { STUIDStr = IDArr });
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
///
/// 两表联合查询
///
///
[HttpGet]
public IHttpActionResult GetStudentAndClass()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"SELECT * FROM STUDENT A JOIN CLASS B ON A.FK_CLASSID = B.ID";
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Query(sql);
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
#endregion
#region 新增
///
/// 插入单条数据(带参数)
///
///
[HttpPost]
public IHttpActionResult AddStudent()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"INSERT INTO STUDENT (NAME,AGE,FK_CLASSID) VALUES (@NAME,@AGE,@CLASSID)";
StudentInfo student = new StudentInfo
{
Name = "恩格斯",
Age = 55,
FK_ClassID = 1
};
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Execute(sql, new { NAME = student.Name, AGE = student.Age, CLASSID = student.FK_ClassID });
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
///
/// 插入单条数据(直接插入整个实体)
///
///
[HttpPost]
public IHttpActionResult AddStudentInfo()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"INSERT INTO STUDENT (NAME,AGE,FK_CLASSID) VALUES (@NAME,@AGE,@FK_CLASSID)";
StudentInfo student = new StudentInfo
{
Name = "马克思",
Age = 55,
FK_ClassID = 1
};
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Execute(sql, student);
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
///
/// 插入多条数据(实体)
///
///
[HttpPost]
public IHttpActionResult AddStudentList()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"INSERT INTO STUDENT (NAME,AGE,FK_CLASSID) VALUES (@NAME,@AGE,@FK_CLASSID)";
List list = new List();
for (int i = 0; i < 3; i++)
{
StudentInfo student = new StudentInfo
{
Name = "强森" + i.ToString(),
Age = 55,
FK_ClassID = 1
};
list.Add(student);
}
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Execute(sql, list);
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
///
/// 插入数据后返回自增主键
///
///
[HttpPost]
public IHttpActionResult AddReturnID()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"INSERT INTO STUDENT (NAME,AGE,FK_CLASSID) VALUES (@NAME,@AGE,@CLASSID)";
StudentInfo student = new StudentInfo
{
Name = "恩格斯",
Age = 55,
FK_ClassID = 1
};
using (IDbConnection conn = new SqlConnection(conStr))
{
sql += "SELECT SCOPE_IDENTITY()";
var result = conn.Execute(sql, new { NAME = student.Name, AGE = student.Age, CLASSID = student.FK_ClassID });
var id = conn.QueryFirstOrDefault(sql, new { NAME = student.Name, AGE = student.Age, CLASSID = student.FK_ClassID });
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, id));
}
}
#endregion
#region 更新
///
/// 使用实体更新
///
///
[HttpPost]
public IHttpActionResult UpdateStudetInfo()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"UPDATE STUDENT SET NAME=@NAME,AGE=@AGE,FK_CLASSID=@FK_CLASSID WHERE STUID = @StuID";
StudentInfo student = new StudentInfo
{
StuID = 1,
Name = "老夫子",
Age = 59,
FK_ClassID = 2
};
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Execute(sql, student);
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
///
/// 参数更新
///
///
[HttpPost]
public IHttpActionResult UpdateStudet(int ID)
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"UPDATE STUDENT SET NAME=@NAME,AGE=@AGE,FK_CLASSID=@FK_CLASSID WHERE STUID = @StuID";
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Execute(sql, new {NAME = "尼古拉斯赵四",AGE = 1,StuID = ID});
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
#endregion
#region 删除
public IHttpActionResult Delete(int ID)
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"DELETE STUDENT WHERE STUID = @StuID";
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Execute(sql, new { StuID = ID });
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
#endregion
#region 事务
[HttpPost]
public IHttpActionResult AddStudentT()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"INSERT INTO STUDENT (NAME,AGE,FK_CLASSID) VALUES (@NAME,@AGE,@CLASSID)";
StudentInfo student = new StudentInfo
{
Name = "恩格斯",
Age = 55,
FK_ClassID = 1
};
StudentInfo student2 = new StudentInfo
{
Name = "恩格斯2",
Age = 55,
FK_ClassID = 1
};
try
{
using (IDbConnection conn = new SqlConnection(conStr))
{
IDbTransaction transaction = conn.BeginTransaction();
var result = conn.Execute(sql, student);
var result1 = conn.Execute(sql, student2);
transaction.Commit();
return Ok(ReturnJsonResult.GetJsonResult(RequestResult.请求成功, result));
}
}
catch (System.Exception)
{
throw;
}
}
#endregion
}
}