First install Visual studio 2015.
Create a New .NET core project
In Option->Nuget Package manager
Add New PackageSource
https://www.myget.org/F/aspnet-contrib/api/v3/index.json
Create a Table
1 2 3 4 5 6 7 8 |
CREATE TABLE [dbo].[Emp]( [Id] [bigint] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NULL, CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] |
in project.json
add dependency
1 |
"Dapper": "1.50.2" |
and Here is the CRUD in Dapper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
public class Emp { public long Id { get; set; } public String Name { get; set; } } String connectionString = @"Server=.\SQLEXPRESS;Database=Test;Persist Security Info=True;User ID=sa;Password=abc"; var conn = new SqlConnection(connectionString); using (IDbConnection dbConnection = conn) { dbConnection.Open(); ////Create Emp emp = new Emp { Name = "akash" }; string insertQuery = @"INSERT INTO Emp (Name) VALUES(@Name)"; int i= dbConnection.Execute(insertQuery, emp); ////Read var epmList = dbConnection.Query<Emp>("SELECT * FROM Emp").ToList(); ////Update //#Custom Data string updateQuery = @"UPDATE Emp SET Name = @Name WHERE Id = @Id"; dbConnection.Query(updateQuery, new { Name = "UpdatedName", Id = 1 }); //#Sepecific Object Emp updatedEmp = new Emp { Id = 4, Name = "xyz" }; dbConnection.Query(updateQuery, updatedEmp); var firstemp = dbConnection.Query<Emp>("SELECT * FROM Emp ").ToList(); ////Delete string deleteQuery = @"DELETE FROM Emp WHERE Id = @Id"; dbConnection.Execute(deleteQuery, new { Id = 1 }); dbConnection.Execute(deleteQuery, new { Id = updatedEmp.Id }); var newEpmList = dbConnection.Query<Emp>("SELECT * FROM Emp").ToList(); } |
#references