Dapper入门教程

什么是Dapper

Dapper是一个简单的.NET对象映射器,在速度方面具有"King of Micro ORM"的头衔,几乎与使用原始的ADO.NET数据读取器一样快。ORM是一个对象关系映射器,它负责数据库和编程语言之间的映射。

Dapper通过扩展IDbConnection提供一些有用的扩展方法去查询您的数据库。

Dapper是如何工作的

它可以分为三个步骤:

  • 创建一个IDbConnection接口对象;
  • 编写一个查询SQL来执行CRUD操作;
  • 将查询SQL作为Execute方法的参数传递。

安装

Dapper

Install-Package Dapper

通过NuGet安装:NuGet Gallery | Dapper 2.0.123

Dapper简单使用

1、连接数据库

使用Mockaroo - Random Data Generator and API Mocking Tool | JSON / CSV / SQL / Excel提供的脚本创建数据库表:

Dapper入门教程_第1张图片

create table Developer (
	id INT,
	DeveloperName VARCHAR(50),
	Email VARCHAR(50),
	GithubURL VARCHAR(50),
	ImageURL VARCHAR(50),
	Department VARCHAR(50),
	JoinedDate DATE
);
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (1, 'Tadeas', '[email protected]', '[email protected]', 'http://dummyimage.com/156x100.png/5fa2dd/ffffff', 'Support', '8/26/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (2, 'Abram', '[email protected]', '[email protected]', 'http://dummyimage.com/145x100.png/cc0000/ffffff', 'Human Resources', '12/22/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (3, 'Rubetta', '[email protected]', '[email protected]', 'http://dummyimage.com/186x100.png/ff4444/ffffff', 'Business Development', '8/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (4, 'Kirbie', '[email protected]', '[email protected]', 'http://dummyimage.com/197x100.png/ff4444/ffffff', 'Legal', '5/16/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (5, 'Florie', '[email protected]', '[email protected]', 'http://dummyimage.com/113x100.png/5fa2dd/ffffff', 'Marketing', '5/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (6, 'Sloane', '[email protected]', '[email protected]', 'http://dummyimage.com/123x100.png/cc0000/ffffff', 'Legal', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (7, 'Dorena', '[email protected]', '[email protected]', 'http://dummyimage.com/190x100.png/dddddd/000000', 'Research and Development', '3/6/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (8, 'Kennett', '[email protected]', '[email protected]', 'http://dummyimage.com/242x100.png/5fa2dd/ffffff', 'Services', '7/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (9, 'Iormina', '[email protected]', '[email protected]', 'http://dummyimage.com/105x100.png/5fa2dd/ffffff', 'Research and Development', '4/25/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (10, 'Gusty', '[email protected]', '[email protected]', 'http://dummyimage.com/157x100.png/cc0000/ffffff', 'Legal', '3/26/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (11, 'Charissa', '[email protected]', '[email protected]', 'http://dummyimage.com/241x100.png/cc0000/ffffff', 'Research and Development', '11/8/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (12, 'Kare', '[email protected]', '[email protected]', 'http://dummyimage.com/233x100.png/5fa2dd/ffffff', 'Training', '12/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (13, 'Skipper', '[email protected]', '[email protected]', 'http://dummyimage.com/136x100.png/dddddd/000000', 'Sales', '10/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (14, 'Hortense', '[email protected]', '[email protected]', 'http://dummyimage.com/140x100.png/dddddd/000000', 'Support', '4/18/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (15, 'Lian', '[email protected]', '[email protected]', 'http://dummyimage.com/183x100.png/ff4444/ffffff', 'Research and Development', '1/29/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (16, 'Madison', '[email protected]', '[email protected]', 'http://dummyimage.com/231x100.png/cc0000/ffffff', 'Human Resources', '10/28/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (17, 'Orville', '[email protected]', '[email protected]', 'http://dummyimage.com/158x100.png/dddddd/000000', 'Sales', '6/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (18, 'Dulcinea', '[email protected]', '[email protected]', 'http://dummyimage.com/168x100.png/dddddd/000000', 'Sales', '7/27/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (19, 'Brett', '[email protected]', '[email protected]', 'http://dummyimage.com/241x100.png/ff4444/ffffff', 'Legal', '2/4/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (20, 'Clerissa', '[email protected]', '[email protected]', 'http://dummyimage.com/210x100.png/ff4444/ffffff', 'Services', '2/6/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (21, 'Darla', '[email protected]', '[email protected]', 'http://dummyimage.com/225x100.png/5fa2dd/ffffff', 'Training', '10/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (22, 'Zeke', '[email protected]', '[email protected]', 'http://dummyimage.com/125x100.png/ff4444/ffffff', 'Product Management', '8/17/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (23, 'Diena', '[email protected]', '[email protected]', 'http://dummyimage.com/135x100.png/5fa2dd/ffffff', 'Support', '10/7/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (24, 'Carri', '[email protected]', '[email protected]', 'http://dummyimage.com/203x100.png/5fa2dd/ffffff', 'Product Management', '4/19/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (25, 'Dale', '[email protected]', '[email protected]', 'http://dummyimage.com/114x100.png/ff4444/ffffff', 'Engineering', '3/11/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (26, 'Allis', '[email protected]', '[email protected]', 'http://dummyimage.com/149x100.png/cc0000/ffffff', 'Sales', '8/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (27, 'Stanly', '[email protected]', '[email protected]', 'http://dummyimage.com/155x100.png/cc0000/ffffff', 'Marketing', '5/15/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (28, 'Godfry', '[email protected]', '[email protected]', 'http://dummyimage.com/226x100.png/ff4444/ffffff', 'Human Resources', '12/25/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (29, 'Norrie', '[email protected]', '[email protected]', 'http://dummyimage.com/135x100.png/ff4444/ffffff', 'Training', '10/16/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (30, 'Sharity', '[email protected]', '[email protected]', 'http://dummyimage.com/103x100.png/5fa2dd/ffffff', 'Legal', '1/12/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (31, 'Cookie', '[email protected]', '[email protected]', 'http://dummyimage.com/134x100.png/ff4444/ffffff', 'Human Resources', '1/24/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (32, 'Gwenora', '[email protected]', '[email protected]', 'http://dummyimage.com/161x100.png/cc0000/ffffff', 'Legal', '10/18/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (33, 'Tara', '[email protected]', '[email protected]', 'http://dummyimage.com/211x100.png/cc0000/ffffff', 'Support', '9/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (34, 'Ramon', '[email protected]', '[email protected]', 'http://dummyimage.com/121x100.png/cc0000/ffffff', 'Legal', '3/19/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (35, 'Renae', '[email protected]', '[email protected]', 'http://dummyimage.com/206x100.png/cc0000/ffffff', 'Accounting', '4/13/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (36, 'Ninon', '[email protected]', '[email protected]', 'http://dummyimage.com/171x100.png/ff4444/ffffff', 'Support', '3/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (37, 'Wyndham', '[email protected]', '[email protected]', 'http://dummyimage.com/237x100.png/ff4444/ffffff', 'Services', '7/10/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (38, 'Merna', '[email protected]', '[email protected]', 'http://dummyimage.com/101x100.png/cc0000/ffffff', 'Engineering', '3/13/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (39, 'Barney', '[email protected]', '[email protected]', 'http://dummyimage.com/232x100.png/5fa2dd/ffffff', 'Business Development', '6/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (40, 'Oralee', '[email protected]', '[email protected]', 'http://dummyimage.com/108x100.png/dddddd/000000', 'Support', '12/4/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (41, 'Leslie', '[email protected]', '[email protected]', 'http://dummyimage.com/138x100.png/ff4444/ffffff', 'Sales', '2/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (42, 'Florance', '[email protected]', '[email protected]', 'http://dummyimage.com/158x100.png/ff4444/ffffff', 'Support', '4/10/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (43, 'Valina', '[email protected]', '[email protected]', 'http://dummyimage.com/205x100.png/5fa2dd/ffffff', 'Sales', '10/28/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (44, 'Gary', '[email protected]', '[email protected]', 'http://dummyimage.com/246x100.png/5fa2dd/ffffff', 'Research and Development', '2/2/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (45, 'Jillian', '[email protected]', '[email protected]', 'http://dummyimage.com/112x100.png/ff4444/ffffff', 'Research and Development', '10/7/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (46, 'Dallas', '[email protected]', '[email protected]', 'http://dummyimage.com/185x100.png/cc0000/ffffff', 'Training', '7/3/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (47, 'Lalo', '[email protected]', '[email protected]', 'http://dummyimage.com/181x100.png/ff4444/ffffff', 'Accounting', '11/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (48, 'Haskel', '[email protected]', '[email protected]', 'http://dummyimage.com/190x100.png/5fa2dd/ffffff', 'Marketing', '9/2/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (49, 'Alison', '[email protected]', '[email protected]', 'http://dummyimage.com/232x100.png/cc0000/ffffff', 'Research and Development', '4/26/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (50, 'Norby', '[email protected]', '[email protected]', 'http://dummyimage.com/194x100.png/cc0000/ffffff', 'Marketing', '12/7/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (51, 'Vonny', '[email protected]', '[email protected]', 'http://dummyimage.com/141x100.png/5fa2dd/ffffff', 'Engineering', '3/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (52, 'Janifer', '[email protected]', '[email protected]', 'http://dummyimage.com/154x100.png/cc0000/ffffff', 'Legal', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (53, 'Thurston', '[email protected]', '[email protected]', 'http://dummyimage.com/246x100.png/ff4444/ffffff', 'Product Management', '7/3/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (54, 'Arlin', '[email protected]', '[email protected]', 'http://dummyimage.com/136x100.png/ff4444/ffffff', 'Sales', '4/21/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (55, 'Davina', '[email protected]', '[email protected]', 'http://dummyimage.com/197x100.png/dddddd/000000', 'Support', '5/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (56, 'Tallou', '[email protected]', '[email protected]', 'http://dummyimage.com/141x100.png/5fa2dd/ffffff', 'Engineering', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (57, 'Gilligan', '[email protected]', '[email protected]', 'http://dummyimage.com/183x100.png/5fa2dd/ffffff', 'Human Resources', '8/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (58, 'Fraze', '[email protected]', '[email protected]', 'http://dummyimage.com/105x100.png/dddddd/000000', 'Human Resources', '10/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (59, 'Wendy', '[email protected]', '[email protected]', 'http://dummyimage.com/115x100.png/cc0000/ffffff', 'Business Development', '12/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (60, 'Shauna', '[email protected]', '[email protected]', 'http://dummyimage.com/176x100.png/dddddd/000000', 'Engineering', '8/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (61, 'Maximilianus', '[email protected]', '[email protected]', 'http://dummyimage.com/165x100.png/5fa2dd/ffffff', 'Marketing', '10/13/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (62, 'Edin', '[email protected]', '[email protected]', 'http://dummyimage.com/188x100.png/ff4444/ffffff', 'Engineering', '9/15/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (63, 'Jaquenetta', '[email protected]', '[email protected]', 'http://dummyimage.com/219x100.png/ff4444/ffffff', 'Human Resources', '3/14/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (64, 'Mischa', '[email protected]', '[email protected]', 'http://dummyimage.com/162x100.png/5fa2dd/ffffff', 'Accounting', '2/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (65, 'Janet', '[email protected]', '[email protected]', 'http://dummyimage.com/190x100.png/dddddd/000000', 'Business Development', '8/18/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (66, 'Rodina', '[email protected]', '[email protected]', 'http://dummyimage.com/136x100.png/cc0000/ffffff', 'Training', '12/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (67, 'Hollie', '[email protected]', '[email protected]', 'http://dummyimage.com/102x100.png/dddddd/000000', 'Legal', '2/20/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (68, 'Devin', '[email protected]', '[email protected]', 'http://dummyimage.com/221x100.png/ff4444/ffffff', 'Legal', '9/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (69, 'Donn', '[email protected]', '[email protected]', 'http://dummyimage.com/153x100.png/ff4444/ffffff', 'Legal', '10/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (70, 'Tybi', '[email protected]', '[email protected]', 'http://dummyimage.com/224x100.png/5fa2dd/ffffff', 'Product Management', '8/23/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (71, 'Melva', '[email protected]', '[email protected]', 'http://dummyimage.com/111x100.png/cc0000/ffffff', 'Training', '2/5/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (72, 'Cointon', '[email protected]', '[email protected]', 'http://dummyimage.com/223x100.png/dddddd/000000', 'Product Management', '3/27/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (73, 'Melina', '[email protected]', '[email protected]', 'http://dummyimage.com/167x100.png/ff4444/ffffff', 'Accounting', '8/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (74, 'Annissa', '[email protected]', '[email protected]', 'http://dummyimage.com/248x100.png/cc0000/ffffff', 'Business Development', '12/8/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (75, 'Cheston', '[email protected]', '[email protected]', 'http://dummyimage.com/125x100.png/cc0000/ffffff', 'Accounting', '11/21/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (76, 'Simone', '[email protected]', '[email protected]', 'http://dummyimage.com/115x100.png/dddddd/000000', 'Research and Development', '2/4/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (77, 'Billy', '[email protected]', '[email protected]', 'http://dummyimage.com/236x100.png/ff4444/ffffff', 'Services', '5/4/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (78, 'Ethan', '[email protected]', '[email protected]', 'http://dummyimage.com/144x100.png/dddddd/000000', 'Support', '10/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (79, 'Thornton', '[email protected]', '[email protected]', 'http://dummyimage.com/213x100.png/cc0000/ffffff', 'Services', '4/28/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (80, 'Ruy', '[email protected]', '[email protected]', 'http://dummyimage.com/182x100.png/cc0000/ffffff', 'Engineering', '10/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (81, 'Ella', '[email protected]', '[email protected]', 'http://dummyimage.com/231x100.png/ff4444/ffffff', 'Accounting', '5/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (82, 'Charlie', '[email protected]', '[email protected]', 'http://dummyimage.com/168x100.png/5fa2dd/ffffff', 'Services', '10/22/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (83, 'Tobi', '[email protected]', '[email protected]', 'http://dummyimage.com/212x100.png/cc0000/ffffff', 'Services', '4/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (84, 'Rani', '[email protected]', '[email protected]', 'http://dummyimage.com/169x100.png/dddddd/000000', 'Accounting', '2/1/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (85, 'Evangeline', '[email protected]', '[email protected]', 'http://dummyimage.com/148x100.png/ff4444/ffffff', 'Services', '1/22/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (86, 'Sophie', '[email protected]', '[email protected]', 'http://dummyimage.com/139x100.png/5fa2dd/ffffff', 'Services', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (87, 'Aimee', '[email protected]', '[email protected]', 'http://dummyimage.com/115x100.png/dddddd/000000', 'Engineering', '10/6/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (88, 'Velma', '[email protected]', '[email protected]', 'http://dummyimage.com/123x100.png/dddddd/000000', 'Training', '10/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (89, 'Shaylah', '[email protected]', '[email protected]', 'http://dummyimage.com/167x100.png/dddddd/000000', 'Sales', '2/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (90, 'Lyda', '[email protected]', '[email protected]', 'http://dummyimage.com/155x100.png/ff4444/ffffff', 'Training', '2/8/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (91, 'Vassily', '[email protected]', '[email protected]', 'http://dummyimage.com/249x100.png/cc0000/ffffff', 'Human Resources', '4/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (92, 'Keane', '[email protected]', '[email protected]', 'http://dummyimage.com/176x100.png/ff4444/ffffff', 'Business Development', '11/25/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (93, 'Stephie', '[email protected]', '[email protected]', 'http://dummyimage.com/119x100.png/5fa2dd/ffffff', 'Business Development', '6/4/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (94, 'Edmon', '[email protected]', '[email protected]', 'http://dummyimage.com/197x100.png/cc0000/ffffff', 'Sales', '2/22/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (95, 'Golda', '[email protected]', '[email protected]', 'http://dummyimage.com/155x100.png/cc0000/ffffff', 'Accounting', '8/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (96, 'Robinett', '[email protected]', '[email protected]', 'http://dummyimage.com/171x100.png/5fa2dd/ffffff', 'Accounting', '12/21/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (97, 'Daisey', '[email protected]', '[email protected]', 'http://dummyimage.com/194x100.png/5fa2dd/ffffff', 'Business Development', '10/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (98, 'Maddi', '[email protected]', '[email protected]', 'http://dummyimage.com/187x100.png/ff4444/ffffff', 'Sales', '5/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (99, 'Garold', '[email protected]', '[email protected]', 'http://dummyimage.com/185x100.png/5fa2dd/ffffff', 'Marketing', '11/20/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (100, 'Purcell', '[email protected]', '[email protected]', 'http://dummyimage.com/208x100.png/5fa2dd/ffffff', 'Support', '2/11/2022');

 创建.NET Core WEB API项目

Dapper入门教程_第2张图片Dapper入门教程_第3张图片

Dapper入门教程_第4张图片 

Dapper入门教程_第5张图片 

添加好项目引用后,就可以添加相应的连接字符串。

2、Dapper使用

 public class DeveloperRepository : IDeveloperRepository
    {
        private readonly IConfiguration configuration;
        public DeveloperRepository(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        public IDbConnection Connection
        {
            get
            {
                return new SqlConnection(configuration.GetConnectionString("Db"));
            }
        }


        public void AddDeveloper(Developer developer)
        {
            try
            {
                using(IDbConnection dbConnection=Connection)
                {
                    string Id=Guid.NewGuid().ToString("N");
                    dbConnection.Open();
                    string query = @"INSERT INTO Developer(DeveloperName,Email,GithubURL,ImageURL,Department,JoinedDate) VALUES(@DeveloperName,@Email,@GithubURL,@ImageURL,@Department,@JoinedDate)";
                    dbConnection.Execute(query, developer);
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public void DeleteDeveloper(int Id)
        {
            try
            {
                using(IDbConnection dbConnection = Connection)
                {
                    dbConnection.Open();
                    string query = @"DELETE FROM Developer WHERE Id=@Id";
                    dbConnection.Execute(query, new {Id=Id});
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public async Task> GetAllDevelopersAsync()
        {
            try
            {
                using(IDbConnection dbConnection=Connection)
                {
                    dbConnection.Open();
                    string query = @"SELECT Id,DeveloperName,Email,GithubURL,ImageURL,Department,JoinedDate FROM Developer";
                  return   await dbConnection.QueryAsync(query);
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public async  Task GetDeveloperByEmailAsync(string Email)
        {
            try
            {
                using (IDbConnection dbConnection = Connection)
                {
                    dbConnection.Open();
                    string query = "SELECT * FROM Developer WHERE Email=@Email";
                    return await dbConnection.QueryFirstOrDefaultAsync(query, new { Email = Email });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public async  Task GetDeveloperByIdAsync(int Id)
        {
            try
            {
                using (IDbConnection dbConnection = Connection)
                {
                    dbConnection.Open();
                    string query = "SELECT * FROM Developer WHERE Id=@Id";
                    return await dbConnection.QueryFirstOrDefaultAsync(query, new { Id = Id });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateDeveloper(Developer developer)
        {
            try
            {
                using (IDbConnection dbConnection = Connection)
                {
                    dbConnection.Open();
                    string query = @"UPDATE Developer SET DeveloperName=@DeveloperName, Email=@Email, GithubURL=@GithubURL, ImageURL=@ImageURL, Department=@Department, JoinedDate=@JoinedDate";
                    dbConnection.Execute(query, developer);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
  public class DeveloperService : IDeveloperService
    {

        protected readonly IDeveloperRepository _developerRepository;

        public DeveloperService(IDeveloperRepository developerRepository)
        {
            _developerRepository = developerRepository;
        }

        public void AddDeveloper(Developer developer)
        {
            _developerRepository.AddDeveloper(developer);
        }

        public void DeleteDeveloper(int Id)
        {
            _developerRepository.DeleteDeveloper(Id);
        }

        public Task> GetAllDevelopers()
        {
            return _developerRepository.GetAllDevelopersAsync();
        }

        public Task GetDeveloperByEmail(string Email)
        {
            return _developerRepository.GetDeveloperByEmailAsync(Email);
        }

        public Task GetDeveloperById(int Id)
        {
            return _developerRepository.GetDeveloperByIdAsync(Id);
        }

        public void UpdateDeveloper(Developer developer)
        {
            _developerRepository.UpdateDeveloper(developer);
        }
    }

 3、WEB API调用

先添加服务,然后就可以调用。

 [Route("api/[controller]/[action]")]
    [ApiController]
    public class DevelopersController : ControllerBase
    {
        protected readonly IDeveloperService _developerService;

        public DevelopersController(IDeveloperService developerService)
        {
            _developerService = developerService;
        }


        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task GetAllDevelopers()
        {
            var developers = await _developerService.GetAllDevelopers();
            return Ok(developers);
        }

        [Route("[action]")]
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task GetDeveloperById(int Id)
        {
            var developer = await _developerService.GetDeveloperById(Id);
            return Ok(developer);
        }

        [Route("[action]")]
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task GetDeveloperByEmail(string Email)
        {
            var developer = await _developerService.GetDeveloperByEmail(Email);
            return Ok(developer);
        }

        [HttpPost]
        [ProducesResponseType(StatusCodes.Status201Created)]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public IActionResult AddDeveloper([FromBody] Developer developer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            _developerService.AddDeveloper(developer);
            return CreatedAtAction(nameof(GetDeveloperById), new { Id = developer.Id }, developer);

        }

        [HttpPut]
        [ProducesResponseType(StatusCodes.Status201Created)]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public IActionResult UpdateDeveloper([FromBody] Developer developer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            _developerService.UpdateDeveloper(developer);
            return Ok();
        }

        [HttpDelete]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public IActionResult DeleteDeveloper(int Id)
        {
            _developerService.DeleteDeveloper(Id);
            return Ok();
        }
    }

调用结果如下

Dapper入门教程_第6张图片

 

你可能感兴趣的:(数据库,sql,.net)