Dapper入门教程

这篇具有很好参考价值的文章主要介绍了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入门教程

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项目

Dapper入门教程Dapper入门教程

Dapper入门教程 

Dapper入门教程 

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

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();
        }
    }

调用结果如下

Dapper入门教程

 文章来源地址https://www.toymoban.com/news/detail-472929.html

到了这里,关于Dapper入门教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [2]SpinalHDL教程——Scala简单入门

    shell里面输入 文件形式 接下来我们使用 scalac 命令编译它: 编译后我们可以看到目录下生成了 HelloWorld.class 文件,该文件可以在Java Virtual Machine (JVM)上运行。 编译后,我们可以使用以下命令来执行程序: Scala是运行在jvm上面的一款语言,在语法和概念上难免和java会有相似之

    2023年04月20日
    浏览(33)
  • Git快速入门篇—— Windows版本淘宝镜像快速下载安装详细步骤及简单入门教程(附带图文教程)

    前言:我们平时在整理代码的时候,尤其是与别人一起开发项目的时候,常常涉及到代码的更新,因此代码版本问题成了一个很头痛的事。而git正是为了解决这种问题而诞生。本文将详细介绍如何通过淘宝镜像进行git的安装以及git的简单入门技巧。 下一章: git与远程仓库的交

    2024年02月03日
    浏览(87)
  • 【业务工具】MobaXterm入门介绍和简单教程

    本文主要整理自: zhihu:MobaXterm详细使用教程系列一 (作者Gang Tang) 实习用到MobaXterm软件,自己入门扫盲一下。 本篇以了解基础概念和大概使用为主。 MobaXterm是一个SSH客户端,SSH客户端的作用是帮助我们在windows下面连接并操作linux服务器。 SSH是一个远程登录的协议。 SSH 为

    2024年02月06日
    浏览(49)
  • WPF真入门教程23--MVVM简单介绍

            在WPF开发中,经典的编程模式是MVVM,是为WPF量身定做的模式,该模式充分利用了WPF的数据绑定机制,最大限度地降低了Xmal文件和CS文件的耦合度,也就是UI显示和逻辑代码的耦合度,如需要更换界面时,逻辑代码修改很少,甚至不用修改。与WinForm开发相比,我们一般

    2024年02月03日
    浏览(40)
  • 掌握Linux指令和权限:一个入门教程

    语法格式 :ls [选项][目录或者文件] 功能 :对于目录,该命令列出该目录下的所有子目录与文件。对于文件,将列出文件名以及其他信息。 a 列出目录下的所有文件,包括以 . 开头的隐含文件。 -d 将目录象文件一样显示,而不是显示其下的文件。 如:ls –d 指定目录 -i 输出文

    2023年04月23日
    浏览(39)
  • HEL库入门教程:STM32CubeIDE汉化教程 直接在线安装 简单易懂

    STM32cubeIDE安装后如果需要汉化,主要有在线安装和离线安装两种,汉化时在网上了解到其他博主的教程主要以离线安装为主,而我采用的是在线安装的技巧,简单快捷,在这里把经验分享给大家。(头一次写,见谅见谅) 在线安装过程中最主要的问题是下载失败,我这里主要

    2024年02月12日
    浏览(45)
  • Android GlSurfaceView 入门教程 : 绘制一个三角形

    GlSurfaceView 是 Android 中的一个类,继承自 SurfaceView ,是用于显示 OpenGL ES 图形渲染的一个 View 。 OpenGL ES 是一种跨平台的图形 API ,用于渲染 2D 和 3D 图形,也可以将相机的画面显示到 GlSurfaceView 上,从而实现滤镜的效果。 GlSurfaceView 提供了一个可以在 Android 应用程序中绘制

    2024年02月13日
    浏览(40)
  • 【Git 入门教程】第一节、什么是Git?

    在软件开发中,代码的管理和版本控制非常重要。为了更好地管理代码,需要使用一种有效的工具来保证代码的质量和稳定性。而Git正是这样一种工具。 Git是一种分布式版本控制系统,它可以追踪文件的变化,并且可以协同工作。它通过记录文件的修改历史,并在不同时间点

    2023年04月25日
    浏览(38)
  • HC-SR04超级简单教程(快速入门)

    目录 一、模块介绍(个人理解)         1.简单理解         2.该模块的参数 二、HC-SR04的操作 三、代码         1.代码前的注意事项         2.关键代码 四、代码实战效果图  五、结束         HC-SR04是一个超声波测距模块,通过发出超声波然后接收超声波

    2024年02月16日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包