Created
February 14, 2012 19:28
-
-
Save lancscoder/1829462 to your computer and use it in GitHub Desktop.
Dapper Getting Started
This file contains 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 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 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 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 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 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 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 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 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 }); | |
} |
Thanks
I love it.
Thanks.
Very useful. Thanks.
Thanks for the code! 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank You