什么是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提供的脚本创建数据库表:
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', 'tdemchen0@craigslist.org', 'tmccarney0@dropbox.com', 'http://dummyimage.com/156x100.png/5fa2dd/ffffff', 'Support', '8/26/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (2, 'Abram', 'asavaage1@flavors.me', 'adowns1@yelp.com', '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', 'rgaine2@patch.com', 'rpurkins2@bbc.co.uk', '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', 'kkelwaybamber3@live.com', 'kducker3@ucla.edu', 'http://dummyimage.com/197x100.png/ff4444/ffffff', 'Legal', '5/16/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (5, 'Florie', 'fdaubney4@marketwatch.com', 'fpentin4@reuters.com', 'http://dummyimage.com/113x100.png/5fa2dd/ffffff', 'Marketing', '5/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (6, 'Sloane', 'sstothard5@com.com', 'spischoff5@blogtalkradio.com', 'http://dummyimage.com/123x100.png/cc0000/ffffff', 'Legal', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (7, 'Dorena', 'deitter6@nasa.gov', 'divatts6@harvard.edu', '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', 'kheeley7@slate.com', 'kdyche7@hud.gov', 'http://dummyimage.com/242x100.png/5fa2dd/ffffff', 'Services', '7/30/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (9, 'Iormina', 'ilawton8@answers.com', 'iskitral8@sbwire.com', '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', 'gkubec9@wikispaces.com', 'gconti9@unicef.org', 'http://dummyimage.com/157x100.png/cc0000/ffffff', 'Legal', '3/26/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (11, 'Charissa', 'chischkea@usa.gov', 'cmenaula@typepad.com', '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', 'kklugmanb@usgs.gov', 'kcashamb@elegantthemes.com', 'http://dummyimage.com/233x100.png/5fa2dd/ffffff', 'Training', '12/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (13, 'Skipper', 'sradleyc@phoca.cz', 'sdonaldsonc@bizjournals.com', 'http://dummyimage.com/136x100.png/dddddd/000000', 'Sales', '10/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (14, 'Hortense', 'hhinkleyd@yale.edu', 'hcraigmyled@ehow.com', 'http://dummyimage.com/140x100.png/dddddd/000000', 'Support', '4/18/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (15, 'Lian', 'lmialle@google.com', 'lorteauxe@umich.edu', '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', 'mwilshawf@yahoo.com', 'mallredf@livejournal.com', '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', 'oattenbarrowg@unc.edu', 'omacgeffeng@ed.gov', 'http://dummyimage.com/158x100.png/dddddd/000000', 'Sales', '6/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (18, 'Dulcinea', 'dfilpih@cbslocal.com', 'dluckenh@yellowbook.com', 'http://dummyimage.com/168x100.png/dddddd/000000', 'Sales', '7/27/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (19, 'Brett', 'bperaccoi@ted.com', 'bsanperi@ft.com', 'http://dummyimage.com/241x100.png/ff4444/ffffff', 'Legal', '2/4/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (20, 'Clerissa', 'carnellj@google.cn', 'cwildtj@over-blog.com', 'http://dummyimage.com/210x100.png/ff4444/ffffff', 'Services', '2/6/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (21, 'Darla', 'dtintok@skype.com', 'dhadleighk@joomla.org', 'http://dummyimage.com/225x100.png/5fa2dd/ffffff', 'Training', '10/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (22, 'Zeke', 'zdoverl@netlog.com', 'zgrotel@deviantart.com', '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', 'drapom@goo.gl', 'dfroschm@imageshack.us', 'http://dummyimage.com/135x100.png/5fa2dd/ffffff', 'Support', '10/7/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (24, 'Carri', 'cwillen@java.com', 'chavockn@chron.com', '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', 'dfuttero@bbb.org', 'dturfino@youku.com', 'http://dummyimage.com/114x100.png/ff4444/ffffff', 'Engineering', '3/11/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (26, 'Allis', 'agilchristp@mediafire.com', 'acromettp@google.com', 'http://dummyimage.com/149x100.png/cc0000/ffffff', 'Sales', '8/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (27, 'Stanly', 'swaylandq@hao123.com', 'sfearbyq@state.tx.us', 'http://dummyimage.com/155x100.png/cc0000/ffffff', 'Marketing', '5/15/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (28, 'Godfry', 'ghouseleer@shinystat.com', 'gdymocker@friendfeed.com', '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', 'nmcarts@goo.ne.jp', 'nbelones@dailymail.co.uk', 'http://dummyimage.com/135x100.png/ff4444/ffffff', 'Training', '10/16/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (30, 'Sharity', 'swisbyt@bloglovin.com', 'scubberleyt@wufoo.com', 'http://dummyimage.com/103x100.png/5fa2dd/ffffff', 'Legal', '1/12/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (31, 'Cookie', 'cmartlewu@etsy.com', 'cchaundyu@nymag.com', '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', 'grichtv@constantcontact.com', 'gjustisv@themeforest.net', 'http://dummyimage.com/161x100.png/cc0000/ffffff', 'Legal', '10/18/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (33, 'Tara', 'taxonw@mozilla.com', 'tblasdalew@arizona.edu', 'http://dummyimage.com/211x100.png/cc0000/ffffff', 'Support', '9/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (34, 'Ramon', 'rdymokex@columbia.edu', 'rtargex@yahoo.co.jp', 'http://dummyimage.com/121x100.png/cc0000/ffffff', 'Legal', '3/19/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (35, 'Renae', 'rteersy@fc2.com', 'rpittwayy@xrea.com', 'http://dummyimage.com/206x100.png/cc0000/ffffff', 'Accounting', '4/13/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (36, 'Ninon', 'nbeadellz@yahoo.com', 'ndumbarez@wp.com', 'http://dummyimage.com/171x100.png/ff4444/ffffff', 'Support', '3/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (37, 'Wyndham', 'wkeaton10@mlb.com', 'wnockalls10@comsenz.com', 'http://dummyimage.com/237x100.png/ff4444/ffffff', 'Services', '7/10/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (38, 'Merna', 'mflear11@stanford.edu', 'mwreath11@google.co.uk', 'http://dummyimage.com/101x100.png/cc0000/ffffff', 'Engineering', '3/13/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (39, 'Barney', 'bwomack12@edublogs.org', 'bkahen12@ihg.com', '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', 'ogillinghams13@youtu.be', 'oattkins13@list-manage.com', 'http://dummyimage.com/108x100.png/dddddd/000000', 'Support', '12/4/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (41, 'Leslie', 'lwhatley14@businessweek.com', 'lstainburn14@kickstarter.com', 'http://dummyimage.com/138x100.png/ff4444/ffffff', 'Sales', '2/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (42, 'Florance', 'fgripton15@fema.gov', 'fwingatt15@mail.ru', 'http://dummyimage.com/158x100.png/ff4444/ffffff', 'Support', '4/10/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (43, 'Valina', 'viglesia16@scribd.com', 'vmonahan16@storify.com', 'http://dummyimage.com/205x100.png/5fa2dd/ffffff', 'Sales', '10/28/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (44, 'Gary', 'gpantry17@fema.gov', 'gmarfell17@storify.com', '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', 'jsaffle18@craigslist.org', 'joxx18@liveinternet.ru', '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', 'dallchorne19@hibu.com', 'divasechko19@cornell.edu', 'http://dummyimage.com/185x100.png/cc0000/ffffff', 'Training', '7/3/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (47, 'Lalo', 'lhammarberg1a@alibaba.com', 'lcornish1a@behance.net', 'http://dummyimage.com/181x100.png/ff4444/ffffff', 'Accounting', '11/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (48, 'Haskel', 'hspatig1b@guardian.co.uk', 'htreweek1b@woothemes.com', 'http://dummyimage.com/190x100.png/5fa2dd/ffffff', 'Marketing', '9/2/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (49, 'Alison', 'agilburt1c@opensource.org', 'aolahy1c@hatena.ne.jp', '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', 'nkynoch1d@google.com.br', 'ncumbes1d@flavors.me', 'http://dummyimage.com/194x100.png/cc0000/ffffff', 'Marketing', '12/7/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (51, 'Vonny', 'vtoll1e@simplemachines.org', 'vaxell1e@vk.com', 'http://dummyimage.com/141x100.png/5fa2dd/ffffff', 'Engineering', '3/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (52, 'Janifer', 'jlambarth1f@squidoo.com', 'jpersence1f@imageshack.us', 'http://dummyimage.com/154x100.png/cc0000/ffffff', 'Legal', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (53, 'Thurston', 'tbroseman1g@histats.com', 'tsonley1g@tinypic.com', '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', 'awestpfel1h@ehow.com', 'aearney1h@biglobe.ne.jp', 'http://dummyimage.com/136x100.png/ff4444/ffffff', 'Sales', '4/21/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (55, 'Davina', 'dpennycuick1i@plala.or.jp', 'dwallice1i@wikia.com', 'http://dummyimage.com/197x100.png/dddddd/000000', 'Support', '5/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (56, 'Tallou', 'tpabst1j@princeton.edu', 'tbirden1j@deviantart.com', 'http://dummyimage.com/141x100.png/5fa2dd/ffffff', 'Engineering', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (57, 'Gilligan', 'glavender1k@auda.org.au', 'gyouthead1k@ihg.com', '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', 'fcumesky1l@weather.com', 'fangerstein1l@arizona.edu', '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', 'welijahu1m@nifty.com', 'wrude1m@pcworld.com', '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', 'smorigan1n@wix.com', 'smarrett1n@ebay.co.uk', 'http://dummyimage.com/176x100.png/dddddd/000000', 'Engineering', '8/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (61, 'Maximilianus', 'mhadleigh1o@123-reg.co.uk', 'mchannon1o@lulu.com', 'http://dummyimage.com/165x100.png/5fa2dd/ffffff', 'Marketing', '10/13/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (62, 'Edin', 'ewillgress1p@usgs.gov', 'evaudre1p@cbsnews.com', 'http://dummyimage.com/188x100.png/ff4444/ffffff', 'Engineering', '9/15/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (63, 'Jaquenetta', 'jjancar1q@harvard.edu', 'joloshin1q@mashable.com', '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', 'mtouret1r@wiley.com', 'mballantyne1r@scientificamerican.com', 'http://dummyimage.com/162x100.png/5fa2dd/ffffff', 'Accounting', '2/24/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (65, 'Janet', 'jhavenhand1s@ed.gov', 'jgarnall1s@guardian.co.uk', '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', 'rshackelton1t@wired.com', 'rfrith1t@ycombinator.com', 'http://dummyimage.com/136x100.png/cc0000/ffffff', 'Training', '12/1/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (67, 'Hollie', 'hcastillo1u@sciencedirect.com', 'hgiacovazzo1u@narod.ru', 'http://dummyimage.com/102x100.png/dddddd/000000', 'Legal', '2/20/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (68, 'Devin', 'dgaraway1v@acquirethisname.com', 'dtremoille1v@upenn.edu', 'http://dummyimage.com/221x100.png/ff4444/ffffff', 'Legal', '9/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (69, 'Donn', 'ddurak1w@who.int', 'dwybrow1w@amazon.de', 'http://dummyimage.com/153x100.png/ff4444/ffffff', 'Legal', '10/29/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (70, 'Tybi', 'tklaaasen1x@wikispaces.com', 'tdougher1x@elpais.com', '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', 'mwohler1y@istockphoto.com', 'mgoodbody1y@mtv.com', 'http://dummyimage.com/111x100.png/cc0000/ffffff', 'Training', '2/5/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (72, 'Cointon', 'cgunther1z@ebay.com', 'cphinnis1z@nbcnews.com', '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', 'myexley20@webnode.com', 'mraynard20@patch.com', 'http://dummyimage.com/167x100.png/ff4444/ffffff', 'Accounting', '8/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (74, 'Annissa', 'amattiello21@about.me', 'alebbon21@disqus.com', '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', 'cdovydenas22@tmall.com', 'crhymer22@cbsnews.com', 'http://dummyimage.com/125x100.png/cc0000/ffffff', 'Accounting', '11/21/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (76, 'Simone', 'strimnell23@google.fr', 'stye23@vistaprint.com', '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', 'bcollerd24@jiathis.com', 'bbettleson24@elegantthemes.com', 'http://dummyimage.com/236x100.png/ff4444/ffffff', 'Services', '5/4/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (78, 'Ethan', 'edecourcy25@cam.ac.uk', 'eagates25@senate.gov', 'http://dummyimage.com/144x100.png/dddddd/000000', 'Support', '10/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (79, 'Thornton', 'tharvatt26@wikipedia.org', 'tnoice26@instagram.com', 'http://dummyimage.com/213x100.png/cc0000/ffffff', 'Services', '4/28/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (80, 'Ruy', 'rstrutley27@bloomberg.com', 'rpovall27@usgs.gov', 'http://dummyimage.com/182x100.png/cc0000/ffffff', 'Engineering', '10/31/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (81, 'Ella', 'enerne28@walmart.com', 'eschooling28@yellowpages.com', 'http://dummyimage.com/231x100.png/ff4444/ffffff', 'Accounting', '5/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (82, 'Charlie', 'cloiterton29@infoseek.co.jp', 'csplevins29@unesco.org', 'http://dummyimage.com/168x100.png/5fa2dd/ffffff', 'Services', '10/22/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (83, 'Tobi', 'twillers2a@sciencedaily.com', 'tnottle2a@google.com.au', 'http://dummyimage.com/212x100.png/cc0000/ffffff', 'Services', '4/5/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (84, 'Rani', 'rramsden2b@istockphoto.com', 'rpratton2b@w3.org', 'http://dummyimage.com/169x100.png/dddddd/000000', 'Accounting', '2/1/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (85, 'Evangeline', 'ebuttler2c@mapquest.com', 'efragino2c@pen.io', 'http://dummyimage.com/148x100.png/ff4444/ffffff', 'Services', '1/22/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (86, 'Sophie', 'stowell2d@yellowbook.com', 'sohare2d@delicious.com', 'http://dummyimage.com/139x100.png/5fa2dd/ffffff', 'Services', '1/23/2023');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (87, 'Aimee', 'abuttler2e@csmonitor.com', 'ahalgarth2e@cafepress.com', 'http://dummyimage.com/115x100.png/dddddd/000000', 'Engineering', '10/6/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (88, 'Velma', 'vsmy2f@ft.com', 'vtapton2f@a8.net', 'http://dummyimage.com/123x100.png/dddddd/000000', 'Training', '10/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (89, 'Shaylah', 'sisaq2g@squarespace.com', 'slangeren2g@drupal.org', 'http://dummyimage.com/167x100.png/dddddd/000000', 'Sales', '2/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (90, 'Lyda', 'lleal2h@deliciousdays.com', 'llidgate2h@usda.gov', 'http://dummyimage.com/155x100.png/ff4444/ffffff', 'Training', '2/8/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (91, 'Vassily', 'vhinken2i@elegantthemes.com', 'vbarbour2i@engadget.com', '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', 'kdeavenell2j@psu.edu', 'kurling2j@mac.com', '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', 'sparidge2k@jiathis.com', 'sabercromby2k@pcworld.com', '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', 'eswindley2l@slate.com', 'emeacher2l@blogs.com', 'http://dummyimage.com/197x100.png/cc0000/ffffff', 'Sales', '2/22/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (95, 'Golda', 'grhodef2m@sourceforge.net', 'gstapells2m@guardian.co.uk', 'http://dummyimage.com/155x100.png/cc0000/ffffff', 'Accounting', '8/9/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (96, 'Robinett', 'rcoch2n@theglobeandmail.com', 'rcicci2n@t-online.de', 'http://dummyimage.com/171x100.png/5fa2dd/ffffff', 'Accounting', '12/21/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (97, 'Daisey', 'dcridlon2o@ucoz.ru', 'dcoventon2o@blog.com', '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', 'mblunderfield2p@sakura.ne.jp', 'mhambric2p@dropbox.com', 'http://dummyimage.com/187x100.png/ff4444/ffffff', 'Sales', '5/12/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (99, 'Garold', 'gcolthard2q@tmall.com', 'gmcneillie2q@domainmarket.com', 'http://dummyimage.com/185x100.png/5fa2dd/ffffff', 'Marketing', '11/20/2022');
insert into Developer (id, DeveloperName, Email, GithubURL, ImageURL, Department, JoinedDate) values (100, 'Purcell', 'pmilliken2r@flavors.me', 'psysland2r@chron.com', 'http://dummyimage.com/208x100.png/5fa2dd/ffffff', 'Support', '2/11/2022');
创建.NET Core WEB API项目
添加好项目引用后,就可以添加相应的连接字符串。
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<IEnumerable<Developer>> GetAllDevelopersAsync()
{
try
{
using(IDbConnection dbConnection=Connection)
{
dbConnection.Open();
string query = @"SELECT Id,DeveloperName,Email,GithubURL,ImageURL,Department,JoinedDate FROM Developer";
return await dbConnection.QueryAsync<Developer>(query);
}
}
catch(Exception e)
{
throw e;
}
}
public async Task<Developer> GetDeveloperByEmailAsync(string Email)
{
try
{
using (IDbConnection dbConnection = Connection)
{
dbConnection.Open();
string query = "SELECT * FROM Developer WHERE Email=@Email";
return await dbConnection.QueryFirstOrDefaultAsync<Developer>(query, new { Email = Email });
}
}
catch (Exception ex)
{
throw ex;
}
}
public async Task<Developer> GetDeveloperByIdAsync(int Id)
{
try
{
using (IDbConnection dbConnection = Connection)
{
dbConnection.Open();
string query = "SELECT * FROM Developer WHERE Id=@Id";
return await dbConnection.QueryFirstOrDefaultAsync<Developer>(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<IEnumerable<Developer>> GetAllDevelopers()
{
return _developerRepository.GetAllDevelopersAsync();
}
public Task<Developer> GetDeveloperByEmail(string Email)
{
return _developerRepository.GetDeveloperByEmailAsync(Email);
}
public Task<Developer> 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<IActionResult> GetAllDevelopers()
{
var developers = await _developerService.GetAllDevelopers();
return Ok(developers);
}
[Route("[action]")]
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> GetDeveloperById(int Id)
{
var developer = await _developerService.GetDeveloperById(Id);
return Ok(developer);
}
[Route("[action]")]
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> 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();
}
}
调用结果如下