当前位置:网站首页>C dapper basically uses addition, deletion, modification and query transactions, etc
C dapper basically uses addition, deletion, modification and query transactions, etc
2022-04-23 17:10:00 【Tomato Warrior】
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 Inquire about
/// <summary>
/// Query all the data
/// </summary>
/// <returns></returns>
[HttpGet]
public IHttpActionResult GetStudentList()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"SELECT * FROM STUDENT";
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Query<StudentInfo>(sql).ToList();
return Ok(ReturnJsonResult.GetJsonResult(RequestResult. The request is successful , result));
}
}
/// <summary>
/// The query ID A single data ( With parameters )
/// </summary>
/// <returns></returns>
[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<StudentInfo>(sql, new { STUID = ID });
return Ok(ReturnJsonResult.GetJsonResult(RequestResult. The request is successful , result));
}
}
/// <summary>
/// IN Inquire about
/// </summary>
/// <returns></returns>
[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<StudentInfo>(sql, new { STUIDStr = IDArr });
return Ok(ReturnJsonResult.GetJsonResult(RequestResult. The request is successful , result));
}
}
/// <summary>
/// Joint query of two tables
/// </summary>
/// <returns></returns>
[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. The request is successful , result));
}
}
#endregion
#region newly added
/// <summary>
/// Insert a single piece of data ( With parameters )
/// </summary>
/// <returns></returns>
[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 = " Engels ",
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. The request is successful , result));
}
}
/// <summary>
/// Insert a single piece of data ( Insert the whole entity directly )
/// </summary>
/// <returns></returns>
[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 = " Marx ",
Age = 55,
FK_ClassID = 1
};
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Execute(sql, student);
return Ok(ReturnJsonResult.GetJsonResult(RequestResult. The request is successful , result));
}
}
/// <summary>
/// Insert multiple data ( Entity )
/// </summary>
/// <returns></returns>
[HttpPost]
public IHttpActionResult AddStudentList()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"INSERT INTO STUDENT (NAME,AGE,FK_CLASSID) VALUES (@NAME,@AGE,@FK_CLASSID)";
List<StudentInfo> list = new List<StudentInfo>();
for (int i = 0; i < 3; i++)
{
StudentInfo student = new StudentInfo
{
Name = " Johnson " + 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. The request is successful , result));
}
}
/// <summary>
/// Insert data and return auto increment primary key
/// </summary>
/// <returns></returns>
[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 = " Engels ",
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<int>(sql, new { NAME = student.Name, AGE = student.Age, CLASSID = student.FK_ClassID });
return Ok(ReturnJsonResult.GetJsonResult(RequestResult. The request is successful , id));
}
}
#endregion
#region to update
/// <summary>
/// Use entity update
/// </summary>
/// <returns></returns>
[HttpPost]
public IHttpActionResult UpdateStudetInfo()
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"UPDATE STUDENT SET [email protected],[email protected],[email protected]_CLASSID WHERE STUID = @StuID";
StudentInfo student = new StudentInfo
{
StuID = 1,
Name = " The professor ",
Age = 59,
FK_ClassID = 2
};
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Execute(sql, student);
return Ok(ReturnJsonResult.GetJsonResult(RequestResult. The request is successful , result));
}
}
/// <summary>
/// Parameters are updated
/// </summary>
/// <returns></returns>
[HttpPost]
public IHttpActionResult UpdateStudet(int ID)
{
string conStr = ConfigurationManager.AppSettings["SqlConnStr"];
string sql = @"UPDATE STUDENT SET [email protected],[email protected],[email protected]_CLASSID WHERE STUID = @StuID";
using (IDbConnection conn = new SqlConnection(conStr))
{
var result = conn.Execute(sql, new {NAME = " Nicholas Zhao si ",AGE = 1,StuID = ID});
return Ok(ReturnJsonResult.GetJsonResult(RequestResult. The request is successful , result));
}
}
#endregion
#region Delete
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. The request is successful , result));
}
}
#endregion
#region Business
[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 = " Engels ",
Age = 55,
FK_ClassID = 1
};
StudentInfo student2 = new StudentInfo
{
Name = " Engels 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. The request is successful , result));
}
}
catch (System.Exception)
{
throw;
}
}
#endregion
}
}
版权声明
本文为[Tomato Warrior]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553458123.html
边栏推荐
- Promise (I)
- Baidu Map Case - modify map style
- feign报400处理
- ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
- Generate random numbers with high quality and Gaussian distribution
- Expression "func" tSource, object "to expression" func "tSource, object" []
- PHP efficiently reads large files and processes data
- Tencent resolves the address according to the IP address
- VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
- Clickhouse table engine
猜你喜欢
Detailed explanation of the penetration of network security in the shooting range
ASP. NET CORE3. 1. Solution to login failure after identity registers users
Node access to Alipay open platform sandbox to achieve payment function
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
[PROJECT] small hat takeout (8)
Scope and scope chain in JS
Milvus 2.0 质量保障系统详解
1-4 configuration executable script of nodejs installation
Installing labellmg tutorial in Windows
C# Task. Delay and thread The difference between sleep
随机推荐
Get the column name list of the table quickly in Oracle
[C] thoroughly understand the deep copy
Aiot industrial technology panoramic structure - Digital Architecture Design (8)
Log4j output log information to file
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
Go language RPC communication
Clickhouse SQL operation
MySQL modify master database
First knowledge of go language
TypeError: set_ figure_ params() got an unexpected keyword argument ‘figsize‘
Shell-sort命令的使用
Interface document yaml
ClickHouse-数据类型
1-1 NodeJS
Solution architect's small bag - 5 types of architecture diagrams
Go language, array, string, slice
Use between nodejs modules
Multithreaded @ async thread pool
[registration] tf54: engineer growth map and excellent R & D organization building
Detailed explanation of Milvus 2.0 quality assurance system