{"id":139213,"date":"2024-10-31T18:10:34","date_gmt":"2024-10-31T10:10:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/139213\/"},"modified":"2024-10-31T18:10:35","modified_gmt":"2024-10-31T10:10:35","slug":"sql-server%e6%95%b8%e6%93%9a%e5%ba%abdatarelation%e7%9a%84%e6%87%89%e7%94%a8%e7%a4%ba%e4%be%8b%e8%a9%b3%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/139213\/","title":{"rendered":"SQL Server\u6578\u64da\u5eabDataRelation\u7684\u61c9\u7528\u793a\u4f8b\u8a73\u89e3"},"content":{"rendered":"<h1 id=\"sql-server%e6%95%b8%e6%93%9a%e5%ba%abdatarelation%e7%9a%84%e6%87%89%e7%94%a8%e7%a4%ba%e4%be%8b%e8%a9%b3%e8%a7%a3-UwGzsHmccf\">SQL Server\u6578\u64da\u5eabDataRelation\u7684\u61c9\u7528\u793a\u4f8b\u8a73\u89e3<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0cSQL Server\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u7a2e\u529f\u80fd\u4f86\u652f\u6301\u6578\u64da\u7684\u7d44\u7e54\u548c\u7ba1\u7406\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662fDataRelation\u3002DataRelation\u5141\u8a31\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u6578\u64da\u8868\u4e4b\u9593\u5efa\u7acb\u95dc\u806f\uff0c\u9019\u5c0d\u65bc\u6578\u64da\u7684\u67e5\u8a62\u548c\u64cd\u4f5c\u81f3\u95dc\u91cd\u8981\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eDataRelation\u7684\u61c9\u7528\u793a\u4f8b\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u5176\u5728SQL Server\u4e2d\u7684\u4f7f\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afdatarelation%ef%bc%9f-UwGzsHmccf\">\u4ec0\u9ebc\u662fDataRelation\uff1f<\/h2>\n<p>DataRelation\u662fSQL Server\u4e2d\u7528\u65bc\u5b9a\u7fa9\u5169\u500bDataTable\u4e4b\u9593\u95dc\u806f\u7684\u5c0d\u8c61\u3002\u9019\u7a2e\u95dc\u806f\u901a\u5e38\u662f\u57fa\u65bc\u4e3b\u9375\u548c\u5916\u9375\u7684\u95dc\u4fc2\u3002\u901a\u904eDataRelation\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728\u4e0d\u540c\u7684\u6578\u64da\u8868\u4e4b\u9593\u9032\u884c\u6578\u64da\u7684\u67e5\u8a62\u548c\u64cd\u4f5c\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002<\/p>\n<h2 id=\"datarelation%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-UwGzsHmccf\">DataRelation\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<ul>\n<li><strong>\u4e3b\u9375\uff08Primary Key\uff09<\/strong>\uff1a\u552f\u4e00\u6a19\u8b58\u6578\u64da\u8868\u4e2d\u6bcf\u4e00\u884c\u7684\u5b57\u6bb5\u3002<\/li>\n<li><strong>\u5916\u9375\uff08Foreign Key\uff09<\/strong>\uff1a\u5728\u4e00\u500b\u8868\u4e2d\u5f15\u7528\u53e6\u4e00\u500b\u8868\u7684\u4e3b\u9375\u7684\u5b57\u6bb5\u3002<\/li>\n<li><strong>\u95dc\u806f\uff08Relation\uff09<\/strong>\uff1a\u5b9a\u7fa9\u5169\u500b\u8868\u4e4b\u9593\u7684\u908f\u8f2f\u95dc\u4fc2\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%bb%ba%e7%ab%8bdatarelation%e7%9a%84%e6%ad%a5%e9%a9%9f-UwGzsHmccf\">\u5efa\u7acbDataRelation\u7684\u6b65\u9a5f<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5efa\u7acbDataRelation\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5275\u5efaDataSet\u5c0d\u8c61\u3002<\/li>\n<li>\u5275\u5efaDataTable\u5c0d\u8c61\u4e26\u5b9a\u7fa9\u5176\u7d50\u69cb\u3002<\/li>\n<li>\u5c07DataTable\u6dfb\u52a0\u5230DataSet\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528DataRelation\u985e\u4f86\u5b9a\u7fa9\u95dc\u806f\u3002<\/li>\n<li>\u5c07DataRelation\u6dfb\u52a0\u5230DataSet\u4e2d\u3002<\/li>\n<\/ol>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-UwGzsHmccf\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>using System;\nusing System.Data;\n\nclass Program\n{\n    static void Main()\n    {\n        \/\/ \u5275\u5efaDataSet\n        DataSet dataSet = new DataSet();\n\n        \/\/ \u5275\u5efa\u7b2c\u4e00\u500bDataTable\n        DataTable customersTable = new DataTable(\"Customers\");\n        customersTable.Columns.Add(\"CustomerID\", typeof(int));\n        customersTable.Columns.Add(\"CustomerName\", typeof(string));\n        customersTable.PrimaryKey = new DataColumn[] { customersTable.Columns[\"CustomerID\"] };\n\n        \/\/ \u5275\u5efa\u7b2c\u4e8c\u500bDataTable\n        DataTable ordersTable = new DataTable(\"Orders\");\n        ordersTable.Columns.Add(\"OrderID\", typeof(int));\n        ordersTable.Columns.Add(\"CustomerID\", typeof(int));\n        ordersTable.PrimaryKey = new DataColumn[] { ordersTable.Columns[\"OrderID\"] };\n\n        \/\/ \u5c07DataTable\u6dfb\u52a0\u5230DataSet\n        dataSet.Tables.Add(customersTable);\n        dataSet.Tables.Add(ordersTable);\n\n        \/\/ \u5275\u5efaDataRelation\n        DataRelation relation = new DataRelation(\"CustomerOrders\",\n            customersTable.Columns[\"CustomerID\"],\n            ordersTable.Columns[\"CustomerID\"]);\n\n        \/\/ \u5c07DataRelation\u6dfb\u52a0\u5230DataSet\n        dataSet.Relations.Add(relation);\n\n        \/\/ \u6dfb\u52a0\u6578\u64da\n        customersTable.Rows.Add(1, \"John Doe\");\n        ordersTable.Rows.Add(1, 1);\n        ordersTable.Rows.Add(2, 1);\n\n        \/\/ \u67e5\u8a62\u95dc\u806f\u6578\u64da\n        foreach (DataRow customer in customersTable.Rows)\n        {\n            Console.WriteLine(\"Customer: \" + customer[\"CustomerName\"]);\n            foreach (DataRow order in customer.GetChildRows(relation))\n            {\n                Console.WriteLine(\"  Order ID: \" + order[\"OrderID\"]);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"datarelation%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-UwGzsHmccf\">DataRelation\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>DataRelation\u5728\u591a\u7a2e\u5834\u666f\u4e2d\u90fd\u80fd\u767c\u63ee\u91cd\u8981\u4f5c\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>\u5831\u544a\u751f\u6210<\/strong>\uff1a\u5728\u751f\u6210\u5831\u544a\u6642\uff0c\u53ef\u4ee5\u901a\u904eDataRelation\u8f15\u9b06\u5730\u7372\u53d6\u76f8\u95dc\u6578\u64da\u3002<\/li>\n<li><strong>\u6578\u64da\u5206\u6790<\/strong>\uff1a\u5728\u9032\u884c\u6578\u64da\u5206\u6790\u6642\uff0c\u80fd\u5920\u5feb\u901f\u67e5\u8a62\u548c\u95dc\u806f\u4e0d\u540c\u8868\u4e2d\u7684\u6578\u64da\u3002<\/li>\n<li><strong>\u6578\u64da\u5b8c\u6574\u6027<\/strong>\uff1a\u901a\u904e\u5b9a\u7fa9\u95dc\u806f\uff0c\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-UwGzsHmccf\">\u7e3d\u7d50<\/h2>\n<p>DataRelation\u662fSQL Server\u4e2d\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u6578\u64da\u8868\u4e4b\u9593\u5efa\u7acb\u6709\u6548\u7684\u95dc\u806f\u3002\u901a\u904e\u672c\u6587\u7684\u793a\u4f8b\u548c\u89e3\u91cb\uff0c\u8b80\u8005\u61c9\u8a72\u80fd\u5920\u7406\u89e3\u5982\u4f55\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u4f7f\u7528DataRelation\u4f86\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002\u5982\u679c\u60a8\u5c0d\u65bc\u6578\u64da\u5eab\u7ba1\u7406\u6709\u9032\u4e00\u6b65\u7684\u9700\u6c42\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u642d\u5efa\u60a8\u7684\u6578\u64da\u5eab\u74b0\u5883\uff0c\u4eab\u53d7\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22SQL Server\u6578\u64da\u5eab\u4e2dDataRelation\u7684\u61c9\u7528\u793a\u4f8b\uff0c\u6df1\u5165\u4e86\u89e3\u5982\u4f55\u6709\u6548\u7ba1\u7406\u548c\u95dc\u806f\u6578\u64da\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-139213","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139213","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=139213"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139213\/revisions"}],"predecessor-version":[{"id":139214,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139213\/revisions\/139214"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=139213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=139213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=139213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}