使用Dapper执行插入和更新

我对使用Dapper感兴趣 – 但从我能告诉它只支持查询和执行。 我没有看到Dapper包含插入和更新对象的方法。

鉴于我们的项目(大多数项目?)需要插入和更新,与Dapper一起进行插入和更新的最佳做法是什么?

我们最好不用诉诸参数构build的ADO.NET方法等。

我现在想到的最好的答案是使用LinqToSQL进行插入和更新。 有更好的答案吗?

我们正在考虑build立一些助手,仍然决定API,如果这是核心或不核心。 有关进度,请参阅: https : //code.google.com/archive/p/dapper-dot-net/issues/6 。

同时,您可以执行以下操作

val = "my value"; cnn.Execute("insert Table(val) values(@val)", new {val}); cnn.Execute("update Table set val = @val where Id = @id", new {val, id = 1}); 

诸如此类

另见我的博客文章: 烦人的插入问题

更新

正如在注释中指出的那样,在Dapper.Contrib项目中现在有几种扩展可以使用这些IDbConnection扩展方法的forms:

 T Get<T>(id); IEnumerable<T> GetAll<T>(); int Insert<T>(T obj); int Insert<T>(Enumerable<T> list); bool Update<T>(T obj); bool Update<T>(Enumerable<T> list); bool Delete<T>(T obj); bool Delete<T>(Enumerable<T> list); bool DeleteAll<T>(); 

你可以这样做:

 sqlConnection.Open(); string sqlQuery = "INSERT INTO [dbo].[Customer]([FirstName],[LastName],[Address],[City]) VALUES (@FirstName,@LastName,@Address,@City)"; sqlConnection.Execute(sqlQuery, new { customerEntity.FirstName, customerEntity.LastName, customerEntity.Address, customerEntity.City }); sqlConnection.Close(); 

使用Dapper执行CRUD操作是一件容易的事情。 我已经提到下面的例子可以帮助你在CRUD操作中。

代码为C RUD:

方法1:当你从不同的实体插入值的时候使用这个方法。

 using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDbConnection"].ConnectionString)) { string insertQuery = @"INSERT INTO [dbo].[Customer]([FirstName], [LastName], [State], [City], [IsActive], [CreatedOn]) VALUES (@FirstName, @LastName, @State, @City, @IsActive, @CreatedOn)"; var result = db.Execute(insertQuery, new { customerModel.FirstName, customerModel.LastName, StateModel.State, CityModel.City, isActive, CreatedOn = DateTime.Now }); } 

方法2:当你的实体属性与SQL列名称相同时使用这个方法。 所以,Dapper是一个ORM映射具有匹配SQL列的实体属性。

 using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDbConnection"].ConnectionString)) { string insertQuery = @"INSERT INTO [dbo].[Customer]([FirstName], [LastName], [State], [City], [IsActive], [CreatedOn]) VALUES (@FirstName, @LastName, @State, @City, @IsActive, @CreatedOn)"; var result = db.Execute(insertQuery, customerViewModel); } 

C代码UD:

 using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDbConnection"].ConnectionString)) { string selectQuery = @"SELECT * FROM [dbo].[Customer] WHERE FirstName = @FirstName"; var result = db.Query(selectQuery, new { customerModel.FirstName }); } 

代码CR U D:

 using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDbConnection"].ConnectionString)) { string updateQuery = @"UPDATE [dbo].[Customer] SET IsActive = @IsActive WHERE FirstName = @FirstName AND LastName = @LastName"; var result = db.Execute(updateQuery, new { isActive, customerModel.FirstName, customerModel.LastName }); } 

CRU代码D

 using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDbConnection"].ConnectionString)) { string deleteQuery = @"DELETE FROM [dbo].[Customer] WHERE FirstName = @FirstName AND LastName = @LastName"; var result = db.Execute(deleteQuery, new { customerModel.FirstName, customerModel.LastName }); } 

使用Dapper.Contrib就像这样简单:

插入列表:

 public int Insert(IEnumerable<YourClass> yourClass) { using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); return conn.Insert(yourClass) ; } } 

插入单个:

 public int Insert(YourClass yourClass) { using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); return conn.Insert(yourClass) ; } } 

更新列表:

 public bool Update(IEnumerable<YourClass> yourClass) { using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); return conn.Update(yourClass) ; } } 

更新单:

 public bool Update(YourClass yourClass) { using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); return conn.Update(yourClass) ; } } 

来源: https : //github.com/StackExchange/Dapper/tree/master/Dapper.Contrib