Skip to content

Instantly share code, notes, and snippets.

@lancscoder
Created February 14, 2012 19:28
Show Gist options
  • Save lancscoder/1829462 to your computer and use it in GitHub Desktop.
Save lancscoder/1829462 to your computer and use it in GitHub Desktop.

Revisions

  1. lancscoder revised this gist Feb 18, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Single.cs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // Dapper - Single
    using (var connection = MvcApplication.GetOpenConnection()) {
    var post = connection.Query<Post("select * from posts where id = @id", 
    var post = connection.Query<Post>("select * from posts where id = @id", 
    new { id = 1 }).First();
    }
  2. lancscoder revised this gist Feb 14, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions MiniProfilerConnection.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    return new MvcMiniProfiler.Data.ProfiledDbConnection(connection, MiniProfiler.Current);
  3. lancscoder created this gist Feb 14, 2012.
    8 changes: 8 additions & 0 deletions Connection.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    public class ConnectionFactory {
    public static DbConnection GetOpenConnection() {
    var connection = new SqlConnection("MyConnectionString");
    connection.Open();

    return connection;
    }
    }
    4 changes: 4 additions & 0 deletions Delete.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    // Dapper - Delete
    using (var connection = MvcApplication.GetOpenConnection()) {
    connection.Execute(@"delete from Posts where id = @id", new { id = 1 });
    }
    5 changes: 5 additions & 0 deletions FilteredList.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    // 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();
    }
    11 changes: 11 additions & 0 deletions Insert.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    // 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();
    }
    12 changes: 12 additions & 0 deletions InsertCompact.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    // 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;
    }
    4 changes: 4 additions & 0 deletions List.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    // Dapper – Simple List
    using (var connection = ConnectionFactory.GetOpenConnection()) {
    var posts = connection.Query<Post>("select * from posts").ToList();
    }
    5 changes: 5 additions & 0 deletions Single.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    // Dapper - Single
    using (var connection = MvcApplication.GetOpenConnection()) {
    var post = connection.Query<Post("select * from posts where id = @id", 
    new { id = 1 }).First();
    }
    9 changes: 9 additions & 0 deletions Update.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // 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 });
    }