当前位置:   article > 正文

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提供的脚本创建数据库表:

  1. create table Developer (
  2. id INT,
  3. DeveloperName VARCHAR(50),
  4. Email VARCHAR(50),
  5. GithubURL VARCHAR(50),
  6. ImageURL VARCHAR(50),
  7. Department VARCHAR(50),
  8. JoinedDate DATE
  9. );
  10. 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');
  11. 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');
  12. 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');
  13. 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');
  14. 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');
  15. 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');
  16. 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');
  17. 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');
  18. 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');
  19. 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');
  20. 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');
  21. 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');
  22. 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');
  23. 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');
  24. 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');
  25. 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');
  26. 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');
  27. 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');
  28. 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');
  29. 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');
  30. 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');
  31. 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');
  32. 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');
  33. 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');
  34. 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');
  35. 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');
  36. 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');
  37. 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');
  38. 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');
  39. 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');
  40. 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');
  41. 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');
  42. 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');
  43. 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');
  44. 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');
  45. 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');
  46. 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');
  47. 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');
  48. 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');
  49. 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');
  50. 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');
  51. 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');
  52. 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');
  53. 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');
  54. 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');
  55. 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');
  56. 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');
  57. 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');
  58. 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');
  59. 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');
  60. 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');
  61. 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');
  62. 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');
  63. 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');
  64. 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');
  65. 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');
  66. 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');
  67. 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');
  68. 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');
  69. 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');
  70. 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');
  71. 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');
  72. 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');
  73. 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');
  74. 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');
  75. 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');
  76. 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');
  77. 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');
  78. 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');
  79. 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');
  80. 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');
  81. 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');
  82. 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');
  83. 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');
  84. 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');
  85. 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');
  86. 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');
  87. 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');
  88. 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');
  89. 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');
  90. 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');
  91. 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');
  92. 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');
  93. 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');
  94. 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');
  95. 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');
  96. 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');
  97. 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');
  98. 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');
  99. 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');
  100. 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');
  101. 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');
  102. 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');
  103. 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');
  104. 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');
  105. 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');
  106. 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');
  107. 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');
  108. 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');
  109. 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使用

  1. public class DeveloperRepository : IDeveloperRepository
  2. {
  3. private readonly IConfiguration configuration;
  4. public DeveloperRepository(IConfiguration configuration)
  5. {
  6. this.configuration = configuration;
  7. }
  8. public IDbConnection Connection
  9. {
  10. get
  11. {
  12. return new SqlConnection(configuration.GetConnectionString("Db"));
  13. }
  14. }
  15. public void AddDeveloper(Developer developer)
  16. {
  17. try
  18. {
  19. using(IDbConnection dbConnection=Connection)
  20. {
  21. string Id=Guid.NewGuid().ToString("N");
  22. dbConnection.Open();
  23. string query = @"INSERT INTO Developer(DeveloperName,Email,GithubURL,ImageURL,Department,JoinedDate) VALUES(@DeveloperName,@Email,@GithubURL,@ImageURL,@Department,@JoinedDate)";
  24. dbConnection.Execute(query, developer);
  25. }
  26. }
  27. catch(Exception e)
  28. {
  29. throw e;
  30. }
  31. }
  32. public void DeleteDeveloper(int Id)
  33. {
  34. try
  35. {
  36. using(IDbConnection dbConnection = Connection)
  37. {
  38. dbConnection.Open();
  39. string query = @"DELETE FROM Developer WHERE Id=@Id";
  40. dbConnection.Execute(query, new {Id=Id});
  41. }
  42. }
  43. catch(Exception e)
  44. {
  45. throw e;
  46. }
  47. }
  48. public async Task<IEnumerable<Developer>> GetAllDevelopersAsync()
  49. {
  50. try
  51. {
  52. using(IDbConnection dbConnection=Connection)
  53. {
  54. dbConnection.Open();
  55. string query = @"SELECT Id,DeveloperName,Email,GithubURL,ImageURL,Department,JoinedDate FROM Developer";
  56. return await dbConnection.QueryAsync<Developer>(query);
  57. }
  58. }
  59. catch(Exception e)
  60. {
  61. throw e;
  62. }
  63. }
  64. public async Task<Developer> GetDeveloperByEmailAsync(string Email)
  65. {
  66. try
  67. {
  68. using (IDbConnection dbConnection = Connection)
  69. {
  70. dbConnection.Open();
  71. string query = "SELECT * FROM Developer WHERE Email=@Email";
  72. return await dbConnection.QueryFirstOrDefaultAsync<Developer>(query, new { Email = Email });
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. throw ex;
  78. }
  79. }
  80. public async Task<Developer> GetDeveloperByIdAsync(int Id)
  81. {
  82. try
  83. {
  84. using (IDbConnection dbConnection = Connection)
  85. {
  86. dbConnection.Open();
  87. string query = "SELECT * FROM Developer WHERE Id=@Id";
  88. return await dbConnection.QueryFirstOrDefaultAsync<Developer>(query, new { Id = Id });
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. throw ex;
  94. }
  95. }
  96. public void UpdateDeveloper(Developer developer)
  97. {
  98. try
  99. {
  100. using (IDbConnection dbConnection = Connection)
  101. {
  102. dbConnection.Open();
  103. string query = @"UPDATE Developer SET DeveloperName=@DeveloperName, Email=@Email, GithubURL=@GithubURL, ImageURL=@ImageURL, Department=@Department, JoinedDate=@JoinedDate";
  104. dbConnection.Execute(query, developer);
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. throw ex;
  110. }
  111. }
  112. }
  1. public class DeveloperService : IDeveloperService
  2. {
  3. protected readonly IDeveloperRepository _developerRepository;
  4. public DeveloperService(IDeveloperRepository developerRepository)
  5. {
  6. _developerRepository = developerRepository;
  7. }
  8. public void AddDeveloper(Developer developer)
  9. {
  10. _developerRepository.AddDeveloper(developer);
  11. }
  12. public void DeleteDeveloper(int Id)
  13. {
  14. _developerRepository.DeleteDeveloper(Id);
  15. }
  16. public Task<IEnumerable<Developer>> GetAllDevelopers()
  17. {
  18. return _developerRepository.GetAllDevelopersAsync();
  19. }
  20. public Task<Developer> GetDeveloperByEmail(string Email)
  21. {
  22. return _developerRepository.GetDeveloperByEmailAsync(Email);
  23. }
  24. public Task<Developer> GetDeveloperById(int Id)
  25. {
  26. return _developerRepository.GetDeveloperByIdAsync(Id);
  27. }
  28. public void UpdateDeveloper(Developer developer)
  29. {
  30. _developerRepository.UpdateDeveloper(developer);
  31. }
  32. }

 3、WEB API调用

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

  1. [Route("api/[controller]/[action]")]
  2. [ApiController]
  3. public class DevelopersController : ControllerBase
  4. {
  5. protected readonly IDeveloperService _developerService;
  6. public DevelopersController(IDeveloperService developerService)
  7. {
  8. _developerService = developerService;
  9. }
  10. [HttpGet]
  11. [ProducesResponseType(StatusCodes.Status200OK)]
  12. public async Task<IActionResult> GetAllDevelopers()
  13. {
  14. var developers = await _developerService.GetAllDevelopers();
  15. return Ok(developers);
  16. }
  17. [Route("[action]")]
  18. [HttpGet]
  19. [ProducesResponseType(StatusCodes.Status200OK)]
  20. public async Task<IActionResult> GetDeveloperById(int Id)
  21. {
  22. var developer = await _developerService.GetDeveloperById(Id);
  23. return Ok(developer);
  24. }
  25. [Route("[action]")]
  26. [HttpGet]
  27. [ProducesResponseType(StatusCodes.Status200OK)]
  28. public async Task<IActionResult> GetDeveloperByEmail(string Email)
  29. {
  30. var developer = await _developerService.GetDeveloperByEmail(Email);
  31. return Ok(developer);
  32. }
  33. [HttpPost]
  34. [ProducesResponseType(StatusCodes.Status201Created)]
  35. [ProducesResponseType(StatusCodes.Status200OK)]
  36. public IActionResult AddDeveloper([FromBody] Developer developer)
  37. {
  38. if (!ModelState.IsValid)
  39. {
  40. return BadRequest();
  41. }
  42. _developerService.AddDeveloper(developer);
  43. return CreatedAtAction(nameof(GetDeveloperById), new { Id = developer.Id }, developer);
  44. }
  45. [HttpPut]
  46. [ProducesResponseType(StatusCodes.Status201Created)]
  47. [ProducesResponseType(StatusCodes.Status200OK)]
  48. public IActionResult UpdateDeveloper([FromBody] Developer developer)
  49. {
  50. if (!ModelState.IsValid)
  51. {
  52. return BadRequest();
  53. }
  54. _developerService.UpdateDeveloper(developer);
  55. return Ok();
  56. }
  57. [HttpDelete]
  58. [ProducesResponseType(StatusCodes.Status200OK)]
  59. public IActionResult DeleteDeveloper(int Id)
  60. {
  61. _developerService.DeleteDeveloper(Id);
  62. return Ok();
  63. }
  64. }

调用结果如下

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/76521?site
推荐阅读
相关标签
  

闽ICP备14008679号