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
CREATE DATABASE [FormulaOne]; | |
CREATE TABLE [Drivers] ( | |
[Id] int IDENTITY(1, 1) NOT NULL, | |
[FirstName] varchar(32) NOT NULL, | |
[LastName] varchar(32) NOT NULL, | |
[ChampionTitleCount] int NOT NULL CONSTRAINT [DF_Drivers_ChampionTitleCount] DEFAULT 0, | |
CONSTRAINT [PK_DriverId] PRIMARY KEY ([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
public class Driver | |
{ | |
public int Id { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public int ChampionTitleCount { get; set; } | |
} |
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
var alonso = new Driver | |
{ | |
FirstName = "Fernando", | |
LastName = "Alonso", | |
ChampionTitleCount = 2 | |
}; | |
string sql = $"INSERT INTO [Drivers] ([FirstName], [LastName], [ChampionTitleCount]) VALUES (@FirstName, @LastName, @ChampionTitleCount);"; | |
await connection.ExecuteAsync(sql, alonso); |
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
var hamilton = new Driver | |
{ | |
FirstName = "Lewis", | |
LastName = "Hamilton", | |
ChampionTitleCount = 4 | |
}; | |
string sql = $@"INSERT INTO [Drivers] ([FirstName], [LastName], [ChampionTitleCount]) VALUES (@FirstName, @LastName, @ChampionTitleCount); | |
SELECT CAST(SCOPE_IDENTITY() AS int);"; |
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
string sql = $@"INSERT INTO [Drivers] ([FirstName], [LastName], [ChampionTitleCount]) VALUES (@FirstName, @LastName, @ChampionTitleCount); | |
SELECT CAST(SCOPE_IDENTITY() AS int);"; | |
await connection.ExecuteAsync(sql, new | |
{ | |
FirstName = "Sebastian", | |
LastName = "Vettel", | |
ChampionTitleCount = 4 | |
}); |
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
string sql = $@"SELECT [Id], [FirstName], [LastName], [ChampionTitleCount] FROM [Drivers];"; | |
IEnumerable<Driver> drivers = (await connection.QueryAsync<Driver>(sql)); |
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
string sql = $@"SELECT [Id] FROM [Drivers] WHERE [LastName] = 'Alonso';"; | |
int driverId = (await connection.QueryAsync<int>(sql)).FirstOrDefault(); | |
if (driverId != default) | |
{ | |
sql = $"INSERT INTO [Tracks] ([Title], [BestLapTimeDriverId]) VALUES ('Belgium', @DriverId);"; | |
await connection.ExecuteAsync(sql, new { DriverId = driverId }); | |
} |
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
string sql = $@"SELECT [Tracks].[Id], [Tracks].[Title], | |
[Drivers].[Id], [Drivers].[FirstName], [Drivers].[LastName], [Drivers].[ChampionTitleCount] | |
FROM [Tracks] | |
INNER JOIN [Drivers] | |
ON [Drivers].[Id] = [Tracks].[BestLapTimeDriverId] | |
WHERE [Tracks].[Id] = 1;"; | |
Track track = (await connection.QueryAsync<Track, Driver, Track>(sql, (t, d) => | |
{ | |
t.BestLapTimeDriver = d; |
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
using Dapper.Mapper; |
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
string sql = $@"SELECT [Tracks].[Id], [Tracks].[Title], | |
[Drivers].[Id], [Drivers].[FirstName], [Drivers].[LastName], [Drivers].[ChampionTitleCount] | |
FROM [Tracks] | |
INNER JOIN [Drivers] | |
ON [Drivers].[Id] = [Tracks].[BeastLapTimeDriverId] | |
WHERE [Tracks].[Id] = 1;"; | |
Track track = (await connection.QueryAsync<Track, Driver>(sql)).FirstOrDefault(); |
OlderNewer