Inserting data into the database is one of the CRUD operations that act on an individual row by inserting a row.
The Dapper.Rainbow library provides an Insert
extension method to insert data into the database.
The following example inserts a single new record.
private static void InsertSingleAuthor()
{
using (DbConnection connection = new SqlConnection(ConnectionString))
{
var db = RainbowDatabase.Init(connection, commandTimeout: 2);
Author author = new Author()
{
FirstName = "Mindy",
LastName = "Kaling"
};
db.Authors.Insert(author);
}
}
If you retrieve all the authors from the database, you will see that the above author is added at the end.
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 you will see the following output.
Cardinal Tom B. Erichsen
William Shakespeare
Robert T. Kiyosaki
Mindy Kaling