{"id":166557,"date":"2024-11-05T17:12:42","date_gmt":"2024-11-05T09:12:42","guid":{"rendered":"https:\/\/server.hk\/cnblog\/166557\/"},"modified":"2024-11-05T17:12:43","modified_gmt":"2024-11-05T09:12:43","slug":"%e5%bf%ab%e9%80%9f%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e6%89%b9%e9%87%8f%e6%b7%bb%e5%8a%a0%ef%bc%9aasp-net%e6%8a%80%e5%b7%a7%e5%88%86%e4%ba%ab-asp-net%e6%80%8e%e9%ba%bc%e6%89%b9%e9%87%8f%e6%b7%bb","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/166557\/","title":{"rendered":"\u5feb\u901f\u5be6\u73fe\u6578\u64da\u6279\u91cf\u6dfb\u52a0\uff1aASP.NET\u6280\u5de7\u5206\u4eab (asp.net\u600e\u9ebc\u6279\u91cf\u6dfb\u52a0\u6578\u64da\u5eab\u6578\u64da)"},"content":{"rendered":"<h1 id=\"%e5%bf%ab%e9%80%9f%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e6%89%b9%e9%87%8f%e6%b7%bb%e5%8a%a0%ef%bc%9aasp-net%e6%8a%80%e5%b7%a7%e5%88%86%e4%ba%ab-FCOIFNiiZj\">\u5feb\u901f\u5be6\u73fe\u6578\u64da\u6279\u91cf\u6dfb\u52a0\uff1aASP.NET\u6280\u5de7\u5206\u4eab<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u7684\u6279\u91cf\u6dfb\u52a0\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u5f9e\u5916\u90e8\u6578\u64da\u6e90\u5c0e\u5165\u6578\u64da\uff0c\u9084\u662f\u7528\u6236\u5728\u524d\u7aef\u754c\u9762\u4e0a\u6279\u91cf\u4e0a\u50b3\u6578\u64da\uff0cASP.NET\u90fd\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u9ad8\u6548\u5730\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u3002\u672c\u6587\u5c07\u5206\u4eab\u4e00\u4e9b\u5728ASP.NET\u4e2d\u6279\u91cf\u6dfb\u52a0\u6578\u64da\u5eab\u6578\u64da\u7684\u6280\u5de7\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87%e6%89%b9%e9%87%8f%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a%ef%bc%9f-FCOIFNiiZj\">\u70ba\u4ec0\u9ebc\u9078\u64c7\u6279\u91cf\u6dfb\u52a0\u6578\u64da\uff1f<\/h2>\n<p>\u6279\u91cf\u6dfb\u52a0\u6578\u64da\u7684\u4e3b\u8981\u512a\u52e2\u5728\u65bc\u6027\u80fd\u548c\u6548\u7387\u3002\u8207\u9010\u689d\u63d2\u5165\u6578\u64da\u76f8\u6bd4\uff0c\u6279\u91cf\u63d2\u5165\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u8207\u6578\u64da\u5eab\u7684\u4ea4\u4e92\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002\u9019\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u5c24\u70ba\u91cd\u8981\uff0c\u56e0\u70ba\u6bcf\u6b21\u8207\u6578\u64da\u5eab\u7684\u4ea4\u4e92\u90fd\u6703\u6d88\u8017\u6642\u9593\u548c\u8cc7\u6e90\u3002<\/p>\n<h2 id=\"asp-net%e4%b8%ad%e6%89%b9%e9%87%8f%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%96%b9%e6%b3%95-FCOIFNiiZj\">ASP.NET\u4e2d\u6279\u91cf\u6dfb\u52a0\u6578\u64da\u7684\u57fa\u672c\u65b9\u6cd5<\/h2>\n<p>\u5728ASP.NET\u4e2d\uff0c\u6709\u5e7e\u7a2e\u5e38\u898b\u7684\u65b9\u6cd5\u53ef\u4ee5\u5be6\u73fe\u6578\u64da\u7684\u6279\u91cf\u6dfb\u52a0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u6280\u8853\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8entity-framework%e7%9a%84addrange%e6%96%b9%e6%b3%95-FCOIFNiiZj\">1. \u4f7f\u7528Entity Framework\u7684AddRange\u65b9\u6cd5<\/h3>\n<p>Entity Framework\u662f\u4e00\u500b\u5f37\u5927\u7684ORM\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684API\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u4f7f\u7528AddRange\u65b9\u6cd5\u53ef\u4ee5\u8f15\u9b06\u5730\u6279\u91cf\u6dfb\u52a0\u6578\u64da\u3002<\/p>\n<pre><code>\nusing (var context = new YourDbContext())\n{\n    var entities = new List\n    {\n        new YourEntity { Property1 = \"Value1\", Property2 = \"Value2\" },\n        new YourEntity { Property1 = \"Value3\", Property2 = \"Value4\" },\n        \/\/ \u66f4\u591a\u5be6\u9ad4\n    };\n\n    context.YourEntities.AddRange(entities);\n    context.SaveChanges();\n}\n<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8sqlbulkcopy%e9%80%b2%e8%a1%8c%e9%ab%98%e6%95%88%e6%8f%92%e5%85%a5-FCOIFNiiZj\">2. \u4f7f\u7528SqlBulkCopy\u9032\u884c\u9ad8\u6548\u63d2\u5165<\/h3>\n<p>SqlBulkCopy\u662f\u4e00\u500b\u5c08\u9580\u7528\u65bc\u9ad8\u6548\u6279\u91cf\u63d2\u5165\u6578\u64da\u7684\u985e\u3002\u5b83\u53ef\u4ee5\u5c07\u6578\u64da\u5f9e\u4e00\u500b\u4f86\u6e90\u5feb\u901f\u5730\u5beb\u5165\u5230SQL Server\u6578\u64da\u5eab\u4e2d\u3002<\/p>\n<pre><code>\nusing (var bulkCopy = new SqlBulkCopy(connectionString))\n{\n    bulkCopy.DestinationTableName = \"dbo.YourTable\";\n\n    \/\/ \u8a2d\u7f6e\u5217\u6620\u5c04\n    bulkCopy.ColumnMappings.Add(\"SourceColumn1\", \"DestinationColumn1\");\n    bulkCopy.ColumnMappings.Add(\"SourceColumn2\", \"DestinationColumn2\");\n\n    \/\/ \u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\n    bulkCopy.WriteToServer(yourDataTable);\n}\n<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e5%ad%98%e5%84%b2%e9%81%8e%e7%a8%8b%e9%80%b2%e8%a1%8c%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5-FCOIFNiiZj\">3. \u4f7f\u7528\u5b58\u5132\u904e\u7a0b\u9032\u884c\u6279\u91cf\u63d2\u5165<\/h3>\n<p>\u5982\u679c\u9700\u8981\u66f4\u9ad8\u7684\u9748\u6d3b\u6027\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u5b58\u5132\u904e\u7a0b\u4f86\u8655\u7406\u6279\u91cf\u63d2\u5165\u3002\u9019\u6a23\u53ef\u4ee5\u5c07\u63d2\u5165\u908f\u8f2f\u5c01\u88dd\u5728\u6578\u64da\u5eab\u4e2d\uff0c\u4e26\u901a\u904eASP.NET\u8abf\u7528\u3002<\/p>\n<pre><code>\nusing (var connection = new SqlConnection(connectionString))\n{\n    using (var command = new SqlCommand(\"YourStoredProcedure\", connection))\n    {\n        command.CommandType = CommandType.StoredProcedure;\n\n        \/\/ \u6dfb\u52a0\u53c3\u6578\n        command.Parameters.AddWithValue(\"@Parameter1\", value1);\n        command.Parameters.AddWithValue(\"@Parameter2\", value2);\n\n        connection.Open();\n        command.ExecuteNonQuery();\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-FCOIFNiiZj\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u4e8b\u52d9\u8655\u7406\uff1a<\/strong>\u5728\u6279\u91cf\u63d2\u5165\u6642\uff0c\u5efa\u8b70\u4f7f\u7528\u4e8b\u52d9\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u7570\u5e38\u8655\u7406\uff1a<\/strong>\u5728\u57f7\u884c\u6279\u91cf\u64cd\u4f5c\u6642\uff0c\u61c9\u8a72\u8003\u616e\u5230\u53ef\u80fd\u51fa\u73fe\u7684\u7570\u5e38\u60c5\u6cc1\uff0c\u4e26\u9032\u884c\u9069\u7576\u7684\u8655\u7406\u3002<\/li>\n<li><strong>\u6027\u80fd\u6e2c\u8a66\uff1a<\/strong>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u5efa\u8b70\u9032\u884c\u6027\u80fd\u6e2c\u8a66\uff0c\u4ee5\u78ba\u4fdd\u9078\u64c7\u7684\u6279\u91cf\u63d2\u5165\u65b9\u6cd5\u80fd\u5920\u6eff\u8db3\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-FCOIFNiiZj\">\u7e3d\u7d50<\/h2>\n<p>\u5728ASP.NET\u4e2d\uff0c\u6279\u91cf\u6dfb\u52a0\u6578\u64da\u662f\u4e00\u500b\u91cd\u8981\u7684\u6280\u8853\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u7121\u8ad6\u662f\u4f7f\u7528Entity Framework\u7684AddRange\u65b9\u6cd5\u3001SqlBulkCopy\u9084\u662f\u5b58\u5132\u904e\u7a0b\uff0c\u958b\u767c\u8005\u90fd\u53ef\u4ee5\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u6848\u3002\u900f\u904e\u9019\u4e9b\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u6578\u64da\uff0c\u63d0\u5347\u61c9\u7528\u7684\u6574\u9ad4\u6027\u80fd\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\u7684ASP.NET\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u548c\u64f4\u5c55\u60a8\u7684\u696d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22ASP.NET\u6280\u5de7\uff0c\u5feb\u901f\u5be6\u73fe\u6578\u64da\u5eab\u6578\u64da\u7684\u6279\u91cf\u6dfb\u52a0\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u7c21\u5316\u6578\u64da\u8655\u7406\u6d41\u7a0b\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-166557","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166557","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=166557"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166557\/revisions"}],"predecessor-version":[{"id":166558,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166557\/revisions\/166558"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=166557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=166557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=166557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}