{"id":58055,"date":"2024-10-12T19:23:16","date_gmt":"2024-10-12T11:23:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58055\/"},"modified":"2024-10-12T19:23:16","modified_gmt":"2024-10-12T11:23:16","slug":"net%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%ef%bc%9a%e5%a4%9a%e7%a8%ae%e6%96%b9%e5%bc%8f%e8%ae%93%e4%bd%a0%e8%bc%95%e9%ac%86%e9%8f%88%e6%8e%a5-net-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58055\/","title":{"rendered":".NET\u6578\u64da\u5eab\u9023\u63a5\uff1a\u591a\u7a2e\u65b9\u5f0f\u8b93\u4f60\u8f15\u9b06\u93c8\u63a5 (.net \u6578\u64da\u5eab\u9023\u63a5\u7684\u65b9\u5f0f)"},"content":{"rendered":"<h1 id=\"net%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%ef%bc%9a%e5%a4%9a%e7%a8%ae%e6%96%b9%e5%bc%8f%e8%ae%93%e4%bd%a0%e8%bc%95%e9%ac%86%e9%8f%88%e6%8e%a5-KFXVaixbGB\">.NET\u6578\u64da\u5eab\u9023\u63a5\uff1a\u591a\u7a2e\u65b9\u5f0f\u8b93\u4f60\u8f15\u9b06\u93c8\u63a5<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u9023\u63a5\u662f\u81f3\u95dc\u91cd\u8981\u7684\u4e00\u74b0\u3002\u5c0d\u65bc\u4f7f\u7528.NET\u6846\u67b6\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u4e86\u89e3\u5982\u4f55\u6709\u6548\u5730\u9023\u63a5\u6578\u64da\u5eab\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u591a\u7a2e.NET\u6578\u64da\u5eab\u9023\u63a5\u7684\u65b9\u5f0f\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u9078\u64c7\u6700\u9069\u5408\u4ed6\u5011\u9700\u6c42\u7684\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"1-ado-net-KFXVaixbGB\">1. ADO.NET<\/h2>\n<p>ADO.NET\u662f.NET\u6846\u67b6\u4e2d\u7528\u65bc\u6578\u64da\u8a2a\u554f\u7684\u6838\u5fc3\u7d44\u4ef6\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7d44\u985e\uff0c\u5141\u8a31\u958b\u767c\u8005\u8207\u5404\u7a2e\u6578\u64da\u6e90\u9032\u884c\u4ea4\u4e92\uff0c\u5305\u62ecSQL Server\u3001Oracle\u548cMySQL\u7b49\u3002ADO.NET\u7684\u4e3b\u8981\u7d44\u4ef6\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>Connection<\/strong>\uff1a\u7528\u65bc\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002<\/li>\n<li><strong>Command<\/strong>\uff1a\u7528\u65bc\u57f7\u884cSQL\u8a9e\u53e5\u6216\u5b58\u5132\u904e\u7a0b\u3002<\/li>\n<li><strong>DataReader<\/strong>\uff1a\u7528\u65bc\u4ee5\u53ea\u9032\u884c\u7684\u65b9\u5f0f\u8b80\u53d6\u6578\u64da\u3002<\/li>\n<li><strong>DataSet<\/strong>\uff1a\u7528\u65bc\u5728\u5167\u5b58\u4e2d\u5b58\u5132\u6578\u64da\u7684\u96c6\u5408\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528ADO.NET\u9023\u63a5SQL Server\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>using System;\nusing System.Data.SqlClient;\n\nclass Program\n{\n    static void Main()\n    {\n        string connectionString = \"Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;\";\n        using (SqlConnection connection = new SqlConnection(connectionString))\n        {\n            connection.Open();\n            SqlCommand command = new SqlCommand(\"SELECT * FROM myTable\", connection);\n            SqlDataReader reader = command.ExecuteReader();\n            while (reader.Read())\n            {\n                Console.WriteLine(reader[0]);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"2-entity-framework-KFXVaixbGB\">2. Entity Framework<\/h2>\n<p>Entity Framework\uff08EF\uff09\u662f\u4e00\u500b\u5f37\u5927\u7684ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u6846\u67b6\uff0c\u7c21\u5316\u4e86\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528LINQ\uff08\u8a9e\u8a00\u96c6\u6210\u67e5\u8a62\uff09\u4f86\u67e5\u8a62\u6578\u64da\uff0c\u4e26\u4e14EF\u6703\u81ea\u52d5\u751f\u6210SQL\u8a9e\u53e5\u3002\u9019\u4f7f\u5f97\u6578\u64da\u5eab\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c0\u548c\u9ad8\u6548\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Entity Framework\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>using (var context = new MyDbContext())\n{\n    var results = from c in context.Customers\n                  where c.City == \"London\"\n                  select c;\n\n    foreach (var customer in results)\n    {\n        Console.WriteLine(customer.Name);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"3-dapper-KFXVaixbGB\">3. Dapper<\/h2>\n<p>Dapper\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684ORM\uff0c\u5c08\u6ce8\u65bc\u6027\u80fd\u548c\u7c21\u55ae\u6027\u3002\u5b83\u6bd4Entity Framework\u66f4\u63a5\u8fd1ADO.NET\uff0c\u4e26\u4e14\u5728\u57f7\u884c\u901f\u5ea6\u4e0a\u8868\u73fe\u512a\u7570\u3002Dapper\u5141\u8a31\u958b\u767c\u8005\u4f7f\u7528\u539f\u751fSQL\u8a9e\u53e5\uff0c\u540c\u6642\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684\u6620\u5c04\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Dapper\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>using (var connection = new SqlConnection(connectionString))\n{\n    var customers = connection.Query&lt;Customer&gt;(\"SELECT * FROM Customers WHERE City = @City\", new { City = \"London\" });\n    foreach (var customer in customers)\n    {\n        Console.WriteLine(customer.Name);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8asp-net-core%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-KFXVaixbGB\">4. \u4f7f\u7528ASP.NET Core\u7684\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728ASP.NET Core\u4e2d\uff0c\u6578\u64da\u5eab\u9023\u63a5\u7684\u65b9\u5f0f\u8207\u50b3\u7d71\u7684ASP.NET\u7565\u6709\u4e0d\u540c\u3002ASP.NET Core\u652f\u6301\u4f9d\u8cf4\u6ce8\u5165\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u5eab\u4e0a\u4e0b\u6587\u7684\u7ba1\u7406\u66f4\u52a0\u9748\u6d3b\u3002\u958b\u767c\u8005\u53ef\u4ee5\u5728Startup.cs\u4e2d\u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u3002<\/p>\n<pre><code>public void ConfigureServices(IServiceCollection services)\n{\n    services.AddDbContext&lt;MyDbContext&gt;(options =&gt;\n        options.UseSqlServer(Configuration.GetConnectionString(\"DefaultConnection\")));\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-KFXVaixbGB\">\u7e3d\u7d50<\/h2>\n<p>\u5728.NET\u958b\u767c\u4e2d\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u7dad\u8b77\u6027\u81f3\u95dc\u91cd\u8981\u3002\u7121\u8ad6\u662f\u4f7f\u7528ADO.NET\u3001Entity Framework\u9084\u662fDapper\uff0c\u6bcf\u7a2e\u65b9\u6cd5\u90fd\u6709\u5176\u7368\u7279\u7684\u512a\u52e2\u548c\u9069\u7528\u5834\u666f\u3002\u958b\u767c\u8005\u61c9\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u6280\u8853\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684.NET\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u591a\u7a2e.NET\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\uff0c\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u4ea4\u4e92\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u8b93\u4f60\u7684\u61c9\u7528\u66f4\u5177\u9748\u6d3b\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-58055","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58055","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=58055"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58055\/revisions"}],"predecessor-version":[{"id":58056,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58055\/revisions\/58056"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}