{"id":58431,"date":"2024-10-12T20:36:38","date_gmt":"2024-10-12T12:36:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58431\/"},"modified":"2024-10-12T20:36:38","modified_gmt":"2024-10-12T12:36:38","slug":"net%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-net%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e4%bb%a3%e7%a2%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58431\/","title":{"rendered":".NET\u9023\u63a5\u6578\u64da\u5eab\u4ee3\u78bc\u793a\u4f8b (.net\u6578\u64da\u5eab\u9023\u63a5\u4ee3\u78bc)"},"content":{"rendered":"<h1 id=\"net%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-net%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e4%bb%a3%e7%a2%bc-bPmRiBfDqN\">.NET\u9023\u63a5\u6578\u64da\u5eab\u4ee3\u78bc\u793a\u4f8b (.NET\u6578\u64da\u5eab\u9023\u63a5\u4ee3\u78bc)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u4f01\u696d\u7cfb\u7d71\uff0c\u6578\u64da\u5eab\u90fd\u626e\u6f14\u8457\u95dc\u9375\u89d2\u8272\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728.NET\u74b0\u5883\u4e2d\u9023\u63a5\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u4ee3\u78bc\u793a\u4f8b\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u9019\u4e00\u904e\u7a0b\u3002<\/p>\n<h2 id=\"1-net%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%a6%82%e8%bf%b0-bPmRiBfDqN\">1. .NET\u6578\u64da\u5eab\u9023\u63a5\u6982\u8ff0<\/h2>\n<p>.NET\u6846\u67b6\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u5f0f\u4f86\u9023\u63a5\u548c\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u6700\u5e38\u7528\u7684\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528ADO.NET\u548cEntity Framework\u3002ADO.NET\u662f\u4e00\u500b\u4f4e\u5c64\u6b21\u7684\u6578\u64da\u8a2a\u554f\u6280\u8853\uff0c\u800cEntity Framework\u5247\u662f\u4e00\u500b\u9ad8\u5c64\u6b21\u7684ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u6846\u67b6\uff0c\u80fd\u5920\u7c21\u5316\u6578\u64da\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8ado-net%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-bPmRiBfDqN\">2. \u4f7f\u7528ADO.NET\u9023\u63a5\u6578\u64da\u5eab<\/h2>\n<p>ADO.NET\u63d0\u4f9b\u4e86\u4e00\u7d44\u985e\u4f86\u8655\u7406\u6578\u64da\u5eab\u7684\u9023\u63a5\u3001\u547d\u4ee4\u548c\u8b80\u53d6\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528ADO.NET\u9023\u63a5SQL Server\u6578\u64da\u5eab\u7684\u57fa\u672c\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=your_server;Database=your_database;User Id=your_username;Password=your_password;\";\n        \n        using (SqlConnection connection = new SqlConnection(connectionString))\n        {\n            try\n            {\n                connection.Open();\n                Console.WriteLine(\"\u6578\u64da\u5eab\u9023\u63a5\u6210\u529f\uff01\");\n\n                \/\/ \u57f7\u884c\u67e5\u8a62\n                string query = \"SELECT * FROM your_table\";\n                SqlCommand command = new SqlCommand(query, connection);\n                SqlDataReader reader = command.ExecuteReader();\n\n                while (reader.Read())\n                {\n                    Console.WriteLine(reader[\"column_name\"].ToString());\n                }\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine(\"\u932f\u8aa4: \" + ex.Message);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u6578\u64da\u5eab\u7684\u9023\u63a5\u5b57\u7b26\u4e32\uff0c\u7136\u5f8c\u4f7f\u7528<code>SqlConnection<\/code>\u985e\u4f86\u5efa\u7acb\u9023\u63a5\u3002\u901a\u904e<code>Open()<\/code>\u65b9\u6cd5\u6253\u958b\u9023\u63a5\uff0c\u4e26\u4f7f\u7528<code>SqlCommand<\/code>\u57f7\u884c\u67e5\u8a62\uff0c\u6700\u5f8c\u4f7f\u7528<code>SqlDataReader<\/code>\u8b80\u53d6\u6578\u64da\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8entity-framework%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-bPmRiBfDqN\">3. \u4f7f\u7528Entity Framework\u9023\u63a5\u6578\u64da\u5eab<\/h2>\n<p>Entity Framework\u4f7f\u5f97\u6578\u64da\u5eab\u64cd\u4f5c\u66f4\u52a0\u7c21\u55ae\u548c\u76f4\u89c0\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Entity Framework\u9023\u63a5\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>using System;\nusing System.Data.Entity;\n\npublic class YourContext : DbContext\n{\n    public DbSet YourEntities { get; set; }\n}\n\npublic class YourEntity\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        using (var context = new YourContext())\n        {\n            var entities = context.YourEntities.ToList();\n            foreach (var entity in entities)\n            {\n                Console.WriteLine(entity.Name);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u4e0a\u4e0b\u6587\u985e<code>YourContext<\/code>\uff0c\u5b83\u7e7c\u627f\u81ea<code>DbContext<\/code>\u3002\u7136\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u901a\u904e\u4e0a\u4e0b\u6587\u4f86\u8a2a\u554f\u6578\u64da\u5eab\u4e2d\u7684\u5be6\u9ad4\uff0c\u4e26\u4f7f\u7528LINQ\u67e5\u8a62\u6578\u64da\u3002<\/p>\n<h2 id=\"4-%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-bPmRiBfDqN\">4. \u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u78ba\u4fdd\u6578\u64da\u5eab\u670d\u52d9\u5668\u6b63\u5728\u904b\u884c\uff0c\u4e26\u4e14\u9023\u63a5\u5b57\u7b26\u4e32\u6b63\u78ba\u3002<\/li>\n<li>\u8655\u7406\u7570\u5e38\u60c5\u6cc1\uff0c\u907f\u514d\u61c9\u7528\u7a0b\u5e8f\u5d29\u6f70\u3002<\/li>\n<li>\u4f7f\u7528<code>using<\/code>\u8a9e\u53e5\u4f86\u78ba\u4fdd\u8cc7\u6e90\u7684\u6b63\u78ba\u91cb\u653e\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-bPmRiBfDqN\">\u7e3d\u7d50<\/h2>\n<p>\u5728.NET\u4e2d\u9023\u63a5\u6578\u64da\u5eab\u662f\u4e00\u500b\u57fa\u672c\u800c\u91cd\u8981\u7684\u6280\u80fd\u3002\u7121\u8ad6\u662f\u4f7f\u7528ADO.NET\u9084\u662fEntity Framework\uff0c\u958b\u767c\u8005\u90fd\u80fd\u5920\u6709\u6548\u5730\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u900f\u904e\u672c\u6587\u63d0\u4f9b\u7684\u4ee3\u78bc\u793a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u4e26\u958b\u59cb\u958b\u767c\u81ea\u5df1\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u4f3a\u670d\u5668\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u9805\u76ee\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22.NET\u9023\u63a5\u6578\u64da\u5eab\u7684\u4ee3\u78bc\u793a\u4f8b\uff0c\u5b78\u7fd2\u5982\u4f55\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\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-58431","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58431","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=58431"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58431\/revisions"}],"predecessor-version":[{"id":58432,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58431\/revisions\/58432"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}