{"id":166655,"date":"2024-11-05T18:13:17","date_gmt":"2024-11-05T10:13:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/166655\/"},"modified":"2024-11-05T18:13:17","modified_gmt":"2024-11-05T10:13:17","slug":"%e5%bf%ab%e9%80%9f%e6%8e%8c%e6%8f%a1-net%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a1%86%e6%9e%b6%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7-net%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/166655\/","title":{"rendered":"\u5feb\u901f\u638c\u63e1.NET\u6578\u64da\u5eab\u6846\u67b6\u4f7f\u7528\u6280\u5de7 (.net\u6578\u64da\u5eab\u6846\u67b6)"},"content":{"rendered":"<h1 id=\"%e5%bf%ab%e9%80%9f%e6%8e%8c%e6%8f%a1-net%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a1%86%e6%9e%b6%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7-net%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a1%86%e6%9e%b6-IXHYgWSLBJ\">\u5feb\u901f\u638c\u63e1.NET\u6578\u64da\u5eab\u6846\u67b6\u4f7f\u7528\u6280\u5de7 (.NET\u6578\u64da\u5eab\u6846\u67b6)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u5c0d\u65bc\u4f7f\u7528.NET\u6846\u67b6\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u638c\u63e1.NET\u6578\u64da\u5eab\u6846\u67b6\u7684\u4f7f\u7528\u6280\u5de7\u5c07\u5927\u5927\u63d0\u9ad8\u958b\u767c\u6548\u7387\u548c\u61c9\u7528\u6027\u80fd\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e.NET\u6578\u64da\u5eab\u6846\u67b6\u7684\u57fa\u672c\u6982\u5ff5\u3001\u5e38\u7528\u6280\u8853\u53ca\u5176\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"net%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a1%86%e6%9e%b6%e6%a6%82%e8%bf%b0-IXHYgWSLBJ\">.NET\u6578\u64da\u5eab\u6846\u67b6\u6982\u8ff0<\/h2>\n<p>.NET\u6578\u64da\u5eab\u6846\u67b6\u4e3b\u8981\u662f\u6307\u4e00\u7cfb\u5217\u7528\u65bc\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u7684API\u548c\u5de5\u5177\u3002\u6700\u5e38\u7528\u7684\u6578\u64da\u5eab\u6846\u67b6\u5305\u62ecADO.NET\u3001Entity Framework\u548cDapper\u7b49\u3002\u9019\u4e9b\u6846\u67b6\u63d0\u4f9b\u4e86\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u529f\u80fd\uff0c\u4f7f\u958b\u767c\u8005\u80fd\u5920\u66f4\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\u7684\u5be6\u73fe\u3002<\/p>\n<h2 id=\"ado-net-IXHYgWSLBJ\">ADO.NET<\/h2>\n<p>ADO.NET\u662f.NET\u6846\u67b6\u4e2d\u6700\u57fa\u672c\u7684\u6578\u64da\u8a2a\u554f\u6280\u8853\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7d44\u985e\u548c\u63a5\u53e3\uff0c\u7528\u65bc\u9023\u63a5\u6578\u64da\u5eab\u3001\u57f7\u884c\u67e5\u8a62\u548c\u8655\u7406\u7d50\u679c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528ADO.NET\u9032\u884c\u57fa\u672c\u6578\u64da\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>using System;\nusing System.Data;\nusing System.Data.SqlClient;\n\nclass Program\n{\n    static void Main()\n    {\n        string connectionString = \"your_connection_string_here\";\n        using (SqlConnection connection = new SqlConnection(connectionString))\n        {\n            connection.Open();\n            SqlCommand command = new SqlCommand(\"SELECT * FROM Users\", connection);\n            SqlDataReader reader = command.ExecuteReader();\n            while (reader.Read())\n            {\n                Console.WriteLine(reader[\"Username\"]);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"entity-framework-IXHYgWSLBJ\">Entity Framework<\/h2>\n<p>Entity Framework (EF) \u662f\u4e00\u500b\u5f37\u5927\u7684ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u6846\u67b6\uff0c\u80fd\u5920\u5c07\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u6620\u5c04\u5230.NET\u5c0d\u8c61\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528LINQ\u67e5\u8a62\u6578\u64da\uff0c\u5f9e\u800c\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Entity Framework\u9032\u884c\u6578\u64da\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>using (var context = new MyDbContext())\n{\n    var users = context.Users.ToList();\n    foreach (var user in users)\n    {\n        Console.WriteLine(user.Username);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"dapper-IXHYgWSLBJ\">Dapper<\/h2>\n<p>Dapper\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684ORM\u6846\u67b6\uff0c\u5c08\u6ce8\u65bc\u6027\u80fd\u548c\u7c21\u55ae\u6027\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u5f0f\u8207ADO.NET\u76f8\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u7c21\u6f54\u7684API\u4f86\u57f7\u884cSQL\u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Dapper\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>using (var connection = new SqlConnection(\"your_connection_string_here\"))\n{\n    connection.Open();\n    var users = connection.Query&lt;User&gt;(\"SELECT * FROM Users\").ToList();\n    foreach (var user in users)\n    {\n        Console.WriteLine(user.Username);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-IXHYgWSLBJ\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u4f7f\u7528\u9023\u63a5\u6c60\uff1a<\/strong>\u78ba\u4fdd\u6578\u64da\u5eab\u9023\u63a5\u7684\u9ad8\u6548\u7ba1\u7406\uff0c\u6e1b\u5c11\u9023\u63a5\u7684\u958b\u92b7\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7570\u6b65\u64cd\u4f5c\uff1a<\/strong>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u4f7f\u7528\u7570\u6b65\u65b9\u6cd5\u4f86\u63d0\u9ad8\u61c9\u7528\u7684\u97ff\u61c9\u6027\u3002<\/li>\n<li><strong>\u9069\u7576\u4f7f\u7528\u4e8b\u52d9\uff1a<\/strong>\u5728\u9700\u8981\u78ba\u4fdd\u6578\u64da\u4e00\u81f4\u6027\u7684\u64cd\u4f5c\u4e2d\uff0c\u4f7f\u7528\u4e8b\u52d9\u4f86\u4fdd\u8b77\u6578\u64da\u7684\u5b8c\u6574\u6027\u3002<\/li>\n<li><strong>\u512a\u5316\u67e5\u8a62\uff1a<\/strong>\u4f7f\u7528\u7d22\u5f15\u548c\u67e5\u8a62\u512a\u5316\u6280\u8853\u4f86\u63d0\u9ad8\u6578\u64da\u6aa2\u7d22\u7684\u6548\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-IXHYgWSLBJ\">\u7e3d\u7d50<\/h2>\n<p>\u638c\u63e1.NET\u6578\u64da\u5eab\u6846\u67b6\u7684\u4f7f\u7528\u6280\u5de7\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\u81f3\u95dc\u91cd\u8981\u3002\u7121\u8ad6\u662f\u4f7f\u7528ADO.NET\u3001Entity Framework\u9084\u662fDapper\uff0c\u4e86\u89e3\u5176\u7279\u6027\u548c\u6700\u4f73\u5be6\u8e10\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u9ad8\u6548\u5730\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002\u82e5\u60a8\u9700\u8981\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0c\u4eab\u53d7\u7a69\u5b9a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u64f4\u5c55\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5feb\u901f\u638c\u63e1.NET\u6578\u64da\u5eab\u6846\u67b6\u4f7f\u7528\u6280\u5de7\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5be6\u73fe\u9ad8\u6548\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-166655","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=166655"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166655\/revisions"}],"predecessor-version":[{"id":166656,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166655\/revisions\/166656"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=166655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=166655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=166655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}