{"id":66187,"date":"2024-10-14T15:17:18","date_gmt":"2024-10-14T07:17:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/66187\/"},"modified":"2024-10-14T15:17:18","modified_gmt":"2024-10-14T07:17:18","slug":"mssql%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%96%b9%e5%bc%8f%e6%af%94%e8%bc%83%ef%bc%9a%e5%82%b3%e7%b5%b1%e8%88%87%e6%96%b0%e5%9e%8b%e6%8a%80%e8%a1%93%ef%bc%88mssql%e9%80%a3%e6%8e%a5%e6%96%b9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/66187\/","title":{"rendered":"MSSQL\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\u6bd4\u8f03\uff1a\u50b3\u7d71\u8207\u65b0\u578b\u6280\u8853\uff08mssql\u9023\u63a5\u65b9\u5f0f\u5340\u5225\uff09"},"content":{"rendered":"<h1 id=\"mssql%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%96%b9%e5%bc%8f%e6%af%94%e8%bc%83%ef%bc%9a%e5%82%b3%e7%b5%b1%e8%88%87%e6%96%b0%e5%9e%8b%e6%8a%80%e8%a1%93-OBSKwLRDPm\">MSSQL\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\u6bd4\u8f03\uff1a\u50b3\u7d71\u8207\u65b0\u578b\u6280\u8853<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u9023\u63a5\u65b9\u5f0f\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u81f3\u95dc\u91cd\u8981\u3002Microsoft SQL Server\uff08MSSQL\uff09\u4f5c\u70ba\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u9023\u63a5\u65b9\u5f0f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u50b3\u7d71\u8207\u65b0\u578b\u6280\u8853\u7684MSSQL\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\uff0c\u4e26\u6bd4\u8f03\u5b83\u5011\u7684\u7279\u9ede\u548c\u9069\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"%e5%82%b3%e7%b5%b1%e9%80%a3%e6%8e%a5%e6%96%b9%e5%bc%8f-OBSKwLRDPm\">\u50b3\u7d71\u9023\u63a5\u65b9\u5f0f<\/h2>\n<p>\u50b3\u7d71\u7684MSSQL\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u5e7e\u7a2e\uff1a<\/p>\n<ul>\n<li><strong>ODBC\uff08Open Database Connectivity\uff09<\/strong><\/li>\n<li><strong>OLE DB\uff08Object Linking and Embedding, Database\uff09<\/strong><\/li>\n<li><strong id=\"ado-net-OBSKwLRDPm\">ADO.NET<\/strong><\/li>\n<\/ul>\n<h3 id=\"odbc-OBSKwLRDPm\">ODBC<\/h3>\n<p>ODBC\u662f\u4e00\u7a2e\u6a19\u6e96\u7684\u6578\u64da\u5eab\u9023\u63a5\u63a5\u53e3\uff0c\u5141\u8a31\u61c9\u7528\u7a0b\u5e8f\u901a\u904e\u7d71\u4e00\u7684API\u8a2a\u554f\u4e0d\u540c\u7684\u6578\u64da\u5eab\u3002\u4f7f\u7528ODBC\u9023\u63a5MSSQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<pre><code>Driver={SQL Server};Server=your_server;Database=your_database;Uid=your_username;Pwd=your_password;<\/code><\/pre>\n<p>ODBC\u7684\u512a\u9ede\u5728\u65bc\u5176\u8de8\u5e73\u53f0\u7684\u7279\u6027\uff0c\u80fd\u5920\u652f\u6301\u591a\u7a2e\u7de8\u7a0b\u8a9e\u8a00\u548c\u64cd\u4f5c\u7cfb\u7d71\u3002\u7136\u800c\uff0cODBC\u7684\u6027\u80fd\u53ef\u80fd\u6703\u53d7\u5230\u9a45\u52d5\u7a0b\u5e8f\u7684\u5f71\u97ff\uff0c\u4e26\u4e14\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u914d\u7f6e\u53ef\u80fd\u8f03\u70ba\u7e41\u7463\u3002<\/p>\n<h3 id=\"ole-db-OBSKwLRDPm\">OLE DB<\/h3>\n<p>OLE DB\u662f\u4e00\u7a2e\u66f4\u70ba\u9748\u6d3b\u7684\u6578\u64da\u8a2a\u554f\u6280\u8853\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u6e90\uff0c\u5305\u62ec\u95dc\u4fc2\u578b\u548c\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u3002OLE DB\u7684\u9023\u63a5\u5b57\u7b26\u4e32\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>Provider=SQLOLEDB;Data Source=your_server;Initial Catalog=your_database;User ID=your_username;Password=your_password;<\/code><\/pre>\n<p>OLE DB\u7684\u512a\u52e2\u5728\u65bc\u5176\u80fd\u5920\u63d0\u4f9b\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u66f4\u8c50\u5bcc\u7684\u529f\u80fd\uff0c\u4f46\u5176\u4f7f\u7528\u7684\u8907\u96dc\u6027\u4e5f\u76f8\u5c0d\u8f03\u9ad8\u3002<\/p>\n<h3 id=\"ado-net-OBSKwLRDPm\">ADO.NET<\/h3>\n<p>ADO.NET\u662f.NET\u6846\u67b6\u4e2d\u7684\u6578\u64da\u8a2a\u554f\u6280\u8853\uff0c\u5c08\u70ba.NET\u61c9\u7528\u7a0b\u5e8f\u8a2d\u8a08\u3002\u4f7f\u7528ADO.NET\u9023\u63a5MSSQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\u4ee3\u78bc\u5982\u4e0b\uff1a<\/p>\n<pre><code>SqlConnection conn = new SqlConnection(\"Server=your_server;Database=your_database;User Id=your_username;Password=your_password;\");\nconn.Open();<\/code><\/pre>\n<p>ADO.NET\u7684\u512a\u52e2\u5728\u65bc\u5176\u8207.NET\u74b0\u5883\u7684\u7dca\u5bc6\u96c6\u6210\uff0c\u80fd\u5920\u63d0\u4f9b\u66f4\u597d\u7684\u6027\u80fd\u548c\u6613\u7528\u6027\u3002<\/p>\n<h2 id=\"%e6%96%b0%e5%9e%8b%e9%80%a3%e6%8e%a5%e6%96%b9%e5%bc%8f-OBSKwLRDPm\">\u65b0\u578b\u9023\u63a5\u65b9\u5f0f<\/h2>\n<p>\u96a8\u8457\u6280\u8853\u7684\u767c\u5c55\uff0c\u65b0\u7684\u6578\u64da\u5eab\u9023\u63a5\u65b9\u5f0f\u4e5f\u9010\u6f38\u51fa\u73fe\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong id=\"entity-framework-OBSKwLRDPm\">Entity Framework<\/strong><\/li>\n<li><strong id=\"linq-to-sql-OBSKwLRDPm\">LINQ to SQL<\/strong><\/li>\n<li><strong id=\"restful-api-OBSKwLRDPm\">RESTful API<\/strong><\/li>\n<\/ul>\n<h3 id=\"entity-framework-OBSKwLRDPm\">Entity Framework<\/h3>\n<p>Entity Framework\uff08EF\uff09\u662f\u4e00\u7a2e\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff08ORM\uff09\u6280\u8853\uff0c\u5141\u8a31\u958b\u767c\u8005\u4f7f\u7528.NET\u5c0d\u8c61\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u4f7f\u7528EF\u9023\u63a5MSSQL\u7684\u793a\u4f8b\u4ee3\u78bc\u5982\u4e0b\uff1a<\/p>\n<pre><code>using (var context = new YourDbContext())\n{\n    var data = context.YourEntities.ToList();\n}<\/code><\/pre>\n<p>EF\u7684\u512a\u52e2\u5728\u65bc\u5176\u80fd\u5920\u7c21\u5316\u6578\u64da\u64cd\u4f5c\uff0c\u4e26\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u4f46\u5728\u6027\u80fd\u4e0a\u53ef\u80fd\u6703\u6709\u6240\u640d\u5931\u3002<\/p>\n<h3 id=\"linq-to-sql-OBSKwLRDPm\">LINQ to SQL<\/h3>\n<p>LINQ to SQL\u662f\u4e00\u7a2e\u5c07LINQ\uff08Language Integrated Query\uff09\u8207SQL Server\u7d50\u5408\u7684\u6280\u8853\uff0c\u5141\u8a31\u958b\u767c\u8005\u4ee5\u66f4\u76f4\u89c0\u7684\u65b9\u5f0f\u67e5\u8a62\u6578\u64da\u3002\u793a\u4f8b\u4ee3\u78bc\u5982\u4e0b\uff1a<\/p>\n<pre><code>var data = from d in db.YourTable\n           where d.Condition == true\n           select d;<\/code><\/pre>\n<p>LINQ to SQL\u7684\u512a\u52e2\u5728\u65bc\u5176\u8a9e\u6cd5\u7c21\u6f54\uff0c\u6613\u65bc\u7406\u89e3\uff0c\u4f46\u50c5\u652f\u6301SQL Server\u3002<\/p>\n<h3 id=\"restful-api-OBSKwLRDPm\">RESTful API<\/h3>\n<p>\u96a8\u8457\u5fae\u670d\u52d9\u67b6\u69cb\u7684\u8208\u8d77\uff0c\u4f7f\u7528RESTful API\u4f86\u8a2a\u554fMSSQL\u6578\u64da\u5eab\u4e5f\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u6d41\u884c\u3002\u9019\u7a2e\u65b9\u5f0f\u901a\u5e38\u6d89\u53ca\u5230\u5c07\u6578\u64da\u5eab\u64cd\u4f5c\u5c01\u88dd\u5728API\u4e2d\uff0c\u901a\u904eHTTP\u8acb\u6c42\u4f86\u9032\u884c\u6578\u64da\u7684\u589e\u522a\u6539\u67e5\u3002\u793a\u4f8b\u8acb\u6c42\u5982\u4e0b\uff1a<\/p>\n<pre><code>GET \/api\/your_endpoint<\/code><\/pre>\n<p>RESTful API\u7684\u512a\u52e2\u5728\u65bc\u5176\u9748\u6d3b\u6027\u548c\u53ef\u64f4\u5c55\u6027\uff0c\u4f46\u9700\u8981\u984d\u5916\u7684\u958b\u767c\u5de5\u4f5c\u4f86\u8a2d\u8a08\u548c\u5be6\u73feAPI\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OBSKwLRDPm\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u9078\u64c7MSSQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u65b9\u5f0f\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u6839\u64da\u5177\u9ad4\u7684\u61c9\u7528\u5834\u666f\u548c\u9700\u6c42\u4f86\u9032\u884c\u9078\u64c7\u3002\u50b3\u7d71\u7684\u9023\u63a5\u65b9\u5f0f\u5982ODBC\u3001OLE DB\u548cADO.NET\u5728\u7a69\u5b9a\u6027\u548c\u517c\u5bb9\u6027\u4e0a\u8868\u73fe\u826f\u597d\uff0c\u800c\u65b0\u578b\u6280\u8853\u5982Entity Framework\u3001LINQ to SQL\u548cRESTful API\u5247\u5728\u958b\u767c\u6548\u7387\u548c\u9748\u6d3b\u6027\u4e0a\u5177\u6709\u512a\u52e2\u3002\u7121\u8ad6\u9078\u64c7\u54ea\u7a2e\u65b9\u5f0f\uff0c\u4e86\u89e3\u5176\u7279\u9ede\u548c\u9069\u7528\u5834\u666f\u90fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u6258\u7ba1\u60a8\u7684MSSQL\u6578\u64da\u5eab\uff0c\u4eab\u53d7\u7a69\u5b9a\u548c\u5b89\u5168\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6bd4\u8f03MSSQL\u6578\u64da\u5eab\u7684\u50b3\u7d71\u8207\u65b0\u578b\u9023\u63a5\u65b9\u5f0f\uff0c\u63a2\u8a0e\u5404\u81ea\u7684\u512a\u7f3a\u9ede\u53ca\u9069\u7528\u5834\u666f\uff0c\u52a9\u60a8\u9078\u64c7\u6700\u4f73\u9023\u63a5\u6280\u8853\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-66187","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66187","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=66187"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66187\/revisions"}],"predecessor-version":[{"id":66188,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66187\/revisions\/66188"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=66187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=66187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=66187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}