Dapper FastCRUD Insert Data


Inserting data into the database is one of the CRUD operations that act on an individual row by inserting a row.

The Dapper.FastCrud library provides Insert extension method to insert data into the database.

The following example inserts a single new record.

private static void InsertSingleAuthor()
{
    using (IDbConnection db = new SqlConnection(ConnectionString))
    {
        Author author = new Author()
        {
            FirstName = "Cokie",
            LastName = "Roberts"
        };

        db.Insert<Author>(author);
    }
}

If you retrieve all the authors from the database, you will see that the above record is inserted at the end.

private static List<Author> GetAllAuthors()
{
    using (IDbConnection db = new SqlConnection(ConnectionString))
    {
        List<Author> authors = db.Find<Author>().ToList();

        return authors;
    }
}

The following code calls the InsertSingleAuthor method and then retrieves all the authors from the database and prints them on the console.

static void Main(string[] args)
{
    InsertSingleAuthor();
    List<Author> authors = GetAllAuthors();

    foreach (var author in authors)
    {
        Console.WriteLine(author.FirstName + " " + author.LastName);
    }
}

Let's execute the above code, and if you retrieve all the authors and books from the database, you will see that the new records are added at the end.

Cardinal Tom B. Erichsen
William Shakespeare
Robert T. Kiyosaki
Cokie Roberts