{"id":171375,"date":"2024-11-07T02:13:21","date_gmt":"2024-11-06T18:13:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/171375\/"},"modified":"2024-11-07T02:13:22","modified_gmt":"2024-11-06T18:13:22","slug":"%e6%b7%ba%e8%ab%87%e8%a8%aa%e5%95%8fsql-azure%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/171375\/","title":{"rendered":"\u6dfa\u8ac7\u8a2a\u554fSQL Azure\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u65b9\u6cd5"},"content":{"rendered":"<h1 id=\"%e6%b7%ba%e8%ab%87%e8%a8%aa%e5%95%8fsql-azure%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e7%9a%84%e6%96%b9%e6%b3%95-MocdZafUGI\">\u6dfa\u8ac7\u8a2a\u554fSQL Azure\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u65b9\u6cd5<\/h1>\n<p>\u96a8\u8457\u96f2\u8a08\u7b97\u6280\u8853\u7684\u8fc5\u901f\u767c\u5c55\uff0c\u8d8a\u4f86\u8d8a\u591a\u7684\u4f01\u696d\u9078\u64c7\u4f7f\u7528\u96f2\u7aef\u6578\u64da\u5eab\u4f86\u5b58\u5132\u548c\u7ba1\u7406\u5176\u6578\u64da\u3002SQL Azure\u4f5c\u70ba\u5fae\u8edfAzure\u5e73\u53f0\u4e0a\u7684\u4e00\u9805\u670d\u52d9\uff0c\u63d0\u4f9b\u4e86\u9ad8\u53ef\u7528\u6027\u548c\u53ef\u64f4\u5c55\u6027\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u8a2a\u554fSQL Azure\u6578\u64da\u5eab\u7684\u5e7e\u7a2e\u5e38\u898b\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u548c\u6578\u64da\u7ba1\u7406\u4eba\u54e1\u66f4\u6709\u6548\u5730\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8azure%e9%96%80%e6%88%b6%e7%b6%b2%e7%ab%99-MocdZafUGI\">1. \u4f7f\u7528Azure\u9580\u6236\u7db2\u7ad9<\/h2>\n<p>Azure\u9580\u6236\u7db2\u7ad9\u662f\u4e00\u500b\u76f4\u89c0\u7684\u5716\u5f62\u7528\u6236\u754c\u9762\uff0c\u5141\u8a31\u7528\u6236\u8f15\u9b06\u7ba1\u7406\u5176Azure\u8cc7\u6e90\u3002\u8a2a\u554fSQL Azure\u6578\u64da\u5eab\u7684\u7b2c\u4e00\u6b65\u662f\u767b\u9304Azure\u9580\u6236\uff0c\u7136\u5f8c\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u5728\u5de6\u5074\u5c0e\u822a\u6b04\u4e2d\u9078\u64c7\u201cSQL\u6578\u64da\u5eab\u201d\u3002<\/li>\n<li>\u9078\u64c7\u60a8\u8981\u8a2a\u554f\u7684\u6578\u64da\u5eab\u3002<\/li>\n<li>\u5728\u6578\u64da\u5eab\u7684\u6982\u89bd\u9801\u9762\u4e2d\uff0c\u60a8\u53ef\u4ee5\u67e5\u770b\u6578\u64da\u5eab\u7684\u57fa\u672c\u4fe1\u606f\u3001\u6027\u80fd\u6307\u6a19\u548c\u9023\u63a5\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p>\u6b64\u5916\uff0cAzure\u9580\u6236\u9084\u63d0\u4f9b\u4e86\u67e5\u8a62\u7de8\u8f2f\u5668\uff0c\u5141\u8a31\u7528\u6236\u76f4\u63a5\u5728\u700f\u89bd\u5668\u4e2d\u57f7\u884cSQL\u67e5\u8a62\uff0c\u9019\u5c0d\u65bc\u5feb\u901f\u6e2c\u8a66\u548c\u8abf\u8a66\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8sql-server-management-studio-ssms-MocdZafUGI\">2. \u4f7f\u7528SQL Server Management Studio (SSMS)<\/h2>\n<p>SQL Server Management Studio (SSMS) \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u684c\u9762\u61c9\u7528\u7a0b\u5e8f\uff0c\u5c08\u70ba\u7ba1\u7406SQL Server\u548cAzure SQL\u6578\u64da\u5eab\u800c\u8a2d\u8a08\u3002\u4f7f\u7528SSMS\u8a2a\u554fSQL Azure\u6578\u64da\u5eab\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u4e0b\u8f09\u4e26\u5b89\u88ddSSMS\u3002<\/li>\n<li>\u555f\u52d5SSMS\uff0c\u4e26\u5728\u201c\u9023\u63a5\u5230\u4f3a\u670d\u5668\u201d\u5c0d\u8a71\u6846\u4e2d\u8f38\u5165\u60a8\u7684SQL Azure\u6578\u64da\u5eab\u7684\u4f3a\u670d\u5668\u540d\u7a31\u3002<\/li>\n<li>\u9078\u64c7\u8eab\u4efd\u9a57\u8b49\u985e\u578b\uff0c\u901a\u5e38\u4f7f\u7528SQL Server\u8eab\u4efd\u9a57\u8b49\uff0c\u7136\u5f8c\u8f38\u5165\u7528\u6236\u540d\u548c\u5bc6\u78bc\u3002<\/li>\n<li>\u9ede\u64ca\u201c\u9023\u63a5\u201d\u4ee5\u8a2a\u554f\u6578\u64da\u5eab\u3002<\/li>\n<\/ul>\n<p>\u4e00\u65e6\u9023\u63a5\u6210\u529f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528SSMS\u7684\u5404\u7a2e\u529f\u80fd\u4f86\u57f7\u884c\u67e5\u8a62\u3001\u7ba1\u7406\u6578\u64da\u5eab\u5c0d\u8c61\u548c\u76e3\u63a7\u6027\u80fd\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8ado-net-MocdZafUGI\">3. \u4f7f\u7528ADO.NET<\/h2>\n<p>\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u4f7f\u7528ADO.NET\u4f86\u8a2a\u554fSQL Azure\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u9748\u6d3b\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684C#\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528ADO.NET\u9023\u63a5\u5230SQL Azure\u6578\u64da\u5eab\u4e26\u57f7\u884c\u67e5\u8a62\uff1a<\/p>\n<pre><code>\nusing System;\nusing System.Data.SqlClient;\n\nclass Program\n{\n    static void Main()\n    {\n        string connectionString = \"Server=tcp:your_server_name.database.windows.net,1433;Initial Catalog=your_database_name;User ID=your_username;Password=your_password;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;\";\n        \n        using (SqlConnection connection = new SqlConnection(connectionString))\n        {\n            connection.Open();\n            SqlCommand command = new SqlCommand(\"SELECT TOP 10 * FROM your_table_name\", connection);\n            SqlDataReader reader = command.ExecuteReader();\n            \n            while (reader.Read())\n            {\n                Console.WriteLine(reader[0].ToString());\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u60a8\u9700\u8981\u66ff\u63db\u9023\u63a5\u5b57\u7b26\u4e32\u4e2d\u7684\u4f54\u4f4d\u7b26\uff0c\u4ee5\u4fbf\u6b63\u78ba\u9023\u63a5\u5230\u60a8\u7684SQL Azure\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8entity-framework-MocdZafUGI\">4. \u4f7f\u7528Entity Framework<\/h2>\n<p>Entity Framework (EF) \u662f\u4e00\u500b\u6d41\u884c\u7684ORM\u6846\u67b6\uff0c\u5141\u8a31\u958b\u767c\u8005\u4ee5\u5c0d\u8c61\u7684\u65b9\u5f0f\u64cd\u4f5c\u6578\u64da\u3002\u4f7f\u7528EF\u8a2a\u554fSQL Azure\u6578\u64da\u5eab\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5728\u60a8\u7684\u9805\u76ee\u4e2d\u5b89\u88ddEntity Framework NuGet\u5305\u3002<\/li>\n<li>\u914d\u7f6e\u60a8\u7684DbContext\u985e\u4ee5\u4f7f\u7528SQL Azure\u9023\u63a5\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u4f7f\u7528LINQ\u67e5\u8a62\u6578\u64da\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\npublic class YourDbContext : DbContext\n{\n    public YourDbContext() : base(\"name=YourConnectionStringName\") { }\n\n    public DbSet YourEntities { get; set; }\n}\n\nclass Program\n{\n    static void Main()\n    {\n        using (var context = new YourDbContext())\n        {\n            var entities = context.YourEntities.ToList();\n            foreach (var entity in entities)\n            {\n                Console.WriteLine(entity.PropertyName);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-MocdZafUGI\">\u7e3d\u7d50<\/h2>\n<p>\u8a2a\u554fSQL Azure\u6578\u64da\u5eab\u7684\u65b9\u6cd5\u591a\u7a2e\u591a\u6a23\uff0c\u7121\u8ad6\u662f\u901a\u904eAzure\u9580\u6236\u3001SSMS\u3001ADO.NET\u9084\u662fEntity Framework\uff0c\u6bcf\u7a2e\u65b9\u6cd5\u90fd\u6709\u5176\u7368\u7279\u7684\u512a\u52e2\u548c\u9069\u7528\u5834\u666f\u3002\u9078\u64c7\u5408\u9069\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u958b\u767c\u6548\u7387\u548c\u6578\u64da\u7ba1\u7406\u7684\u9748\u6d3b\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u548c\u7a69\u5b9a\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u96f2\u670d\u52d9\u63d0\u4f9b\u5546\u81f3\u95dc\u91cd\u8981\u3002\u82e5\u60a8\u5c0d\u65bc<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u6216<code><a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a><\/code>\u6709\u8208\u8da3\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6dfa\u8ac7\u8a2a\u554fSQL Azure\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u63a2\u7d22\u9023\u63a5\u3001\u67e5\u8a62\u53ca\u7ba1\u7406\u6578\u64da\u7684\u6709\u6548\u6280\u5de7\u8207\u6700\u4f73\u5be6\u8e10\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-171375","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171375","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=171375"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171375\/revisions"}],"predecessor-version":[{"id":171376,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171375\/revisions\/171376"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=171375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=171375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=171375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}