-
-
Save ChauThan/c0440d48a98d0e7e9528875901714068 to your computer and use it in GitHub Desktop.
Dapper Getting Started
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConnectionFactory { | |
public static DbConnection GetOpenConnection() { | |
var connection = new SqlConnection("MyConnectionString"); | |
connection.Open(); | |
return connection; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dapper - Delete | |
using (var connection = MvcApplication.GetOpenConnection()) { | |
connection.Execute(@"delete from Posts where id = @id", new { id = 1 }); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dapper – Filtered List | |
using (var connection = ConnectionFactory.GetOpenConnection()) { | |
var posts = connection.Query<Post>("select * from posts where text like @text", | |
new { text = "%Some Value%" }).ToList(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dapper - Insert | |
using (var connection = MvcApplication.GetOpenConnection()) { | |
var post = new Post { | |
Title = "Title 1", | |
Text = "Text 1", | |
PublishDate = DateTime.Now }; | |
post.ID = connection.Query<int>(@"insert into Posts(Title, Text, PublishDate) | |
values (@Title, @Text, @PublishDate); | |
select cast(scope_identity() as int)", | |
new { post.Title, post.Text, post.PublishDate }).First(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dapper Insert Using SQL Compact Edition | |
using (var connection = MvcApplication.GetOpenConnection()) { | |
var post = new Post { | |
Title = "Title 1", | |
Text = "Text 1", | |
PublishDate = DateTime.Now }; | |
connection.Execute(@"insert into Posts(Title, Text, PublishDate) | |
values (@Title, @Text, @PublishDate);", | |
new { post.Title, post.Text, post.PublishDate }); | |
post.ID = (int)connection.Query(@"select @@IDENTITY as ID").First().ID; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dapper – Simple List | |
using (var connection = ConnectionFactory.GetOpenConnection()) { | |
var posts = connection.Query<Post>("select * from posts").ToList(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return new MvcMiniProfiler.Data.ProfiledDbConnection(connection, MiniProfiler.Current); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dapper - Single | |
using (var connection = MvcApplication.GetOpenConnection()) { | |
var post = connection.Query<Post>("select * from posts where id = @id", | |
new { id = 1 }).First(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dapper - Update | |
using (var connection = MvcApplication.GetOpenConnection()) { | |
var post = connection.Query<Post>( | |
"select * from posts where id = @id", new { id = 1 } ).First(); | |
post.Title = "New Title"; | |
connection.Execute(@"update Posts set Title = @Title, | |
Text = @Text, PublishDate = @PublishDate where ID = @id;", | |
new { post.ID, post.Title, post.Text, post.PublishDate }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment