{"id":537,"date":"2009-07-21T22:40:00","date_gmt":"2009-07-21T02:40:00","guid":{"rendered":""},"modified":"2013-11-17T13:19:15","modified_gmt":"2013-11-17T05:19:15","slug":"asp-net%e6%9f%a5%e8%af%a2%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/537.html","title":{"rendered":"ASP.NET\u67e5\u8be2\u6570\u636e\u5e93"},"content":{"rendered":"<p>ADO.NET\u63d0\u4f9b\u4e86Connection\u6765\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u540c\u65f6\u4e5f\u63d0\u4f9b\u4e86Command\u5bf9\u8c61\u6765\u67e5\u8be2\u6570\u636e\u5e93\u3002\u540cConnection\u5bf9\u8c61\u4e00\u6837\uff0cCommand\u4e5f\u6709\u4e24\u79cd\uff1aOleDbCommand\u548cSqlCommand.\u5176\u533a\u522b\u540cConnection<\/p>\n<p>\u5bf9\u8c61\u3002<br \/>\n\u8981\u64cd\u7eb5\u6570\u636e\u5e93\uff0c\u5fc5\u987b\u5148\u4f7f\u7528Connection\u6765\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u518d\u521b\u5efa\u4e00\u4e2aCommand\u6765\u67e5\u8be2\u3002\u6709\u51e0\u79cd\u521b\u5efa\u65b9\u5f0f\uff0c\u4f8b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n   SqlCommand cmd; \r\n   \r\n   string strCon=&quot;server=localhost;database=Northwind;Trusted_Connection=Yes;&quot;; \r\n   string strqry=&quot;select * from Categories&quot;; \r\n   SqlConnection con=new SqlConnection(strCon); \r\n   con.Open(); \r\n   1cmd=con.CreateCommand(); \/\/\u8fd9\u91cc\u4f7f\u7528\u7528Connection\u5bf9\u8c61\u7684CreateCommand\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2aCommand\u5bf9\u8c61\u3002 \r\n   cmd.CommandText=strqry; \r\n   \/\/ SqlDataReader reader=cmd.ExecuteReader(); \r\n   \r\n   2 cmd=new SqlCommand(); \/\/\u76f4\u63a5\u4f7f\u7528new \u5173\u952e\u5b57\u6765\u521b\u5efa \r\n   cmd.CommandText=strqry; \r\n   cmd.Connection=con; \/\/\u8bbe\u7f6e\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5 \r\n   \r\n   3cmd=new SqlCommand(strqry,con);\u3000\/\/\u76f4\u63a5\u5728new\u7684\u65f6\u5019\u5e26\u4e24\u4e2a\u53c2\u6570\u6765\u521b\u5efa \r\n   \r\n<\/pre>\n<p>\u6267\u884c\u65b9\u5f0f\uff1a <\/p>\n<p>\uff08\u4e3b\u8981\u6709\u8fd9\u4e48\u51e0\u79cd\uff0ccmd.ExecuteReader();cmd.ExecuteNonQuery();cmd.ExecuteScalar();cmd.ExecuteXmlReader();\uff09 <\/p>\n<p>\uff11\uff0cExecuteReader();\u8fd4\u56de\u4e00\u4e2aSqlDataReader\u5bf9\u8c61\u6216OleDbDataReader\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u770b\u4f60\u7684\u7a0b\u5e8f\u7684\u9700\u8981\u53bb\u3000\u3000\u505a\u3002\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u5bf9\u8c61\u6765\u68c0\u67e5\u67e5\u8be2\u7ed3\u679c\uff0c\u5b83\u63d0\u4f9b\u4e86\u201c\u6e38\u6c34\u201d\u5f0f\u7684\u6267\u884c\u65b9\u5f0f\uff0c\u5373\u4ece\u7ed3\u679c\u4e2d\u8bfb\u53d6\u4e00\u884c\u4e4b\u540e\uff0c\u79fb\u52a8\u5230\u53e6\u4e00\u884c\uff0c\u5219\u524d\u4e00\u884c\u5c31\u65e0\u6cd5\u518d\u7528\u3002\u6709\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u662f\u6267\u884c\u4e4b\u540e\uff0c\u8981\u7b49\u5230\u624b\u52a8\u53bb\u8c03\u7528Read()\u65b9\u6cd5\u4e4b\u540e\uff0cDataReader\u5bf9\u8c61\u624d\u4f1a\u79fb\u52a8\u5230\u7ed3\u679c\u96c6\u7684\u7b2c\u4e00\u884c\uff0c\u540c\u65f6\u6b64\u65b9\u6cd5\u4e5f\u8fd4\u56de\u4e00\u4e2aBool\u503c\uff0c\u8868\u660e\u4e0b\u4e00\u884c\u662f\u5426\u53ef\u7528\uff0c\u8fd4\u56deTrue\u5219\u53ef\u7528\uff0c\u8fd4\u56deFalse\u5219\u5230\u8fbe\u7ed3\u679c\u96c6\u672b\u5c3e\u3002 <\/p>\n<p>\u4f7f\u7528DataReader\u53ef\u4ee5\u63d0\u9ad8\u6267\u884c\u6548\u7387\uff0c\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\uff1a\u4e00\u79cd\u662f\u57fa\u4e8e\u5e8f\u53f7\u7684\u67e5\u627e\uff0c\u4e00\u4e2a\u662f\u4f7f\u7528\u9002\u5f53\u7684Get\u65b9\u6cd5\u6765\u67e5\u627e\u3002\u56e0\u4e3a\u67e5\u8be2\u51fa\u6765\u7684\u7ed3\u679c\u4e00\u822c\u90fd\u4e0d\u4f1a\u6539\u53d8\uff0c\u9664\u975e\u518d\u6b21\u6539\u52a8\u67e5\u8be2\u8bed\u53e5\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4f4d\u5217\u7684\u4f4d\u7f6e\u6765\u67e5\u627e\u8bb0\u5f55\u3002\u7528\u8fd9\u79cd\u65b9\u6cd5\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f\u53ef\u80fd\u77e5\u9053\u4e00\u5217\u7684\u540d\u79f0\u800c\u4e0d\u77e5\u9053\u5176\u6240\u5728\u7684\u4f4d\u7f6e\uff0c\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u662f\u901a\u8fc7\u8c03\u7528DataReader \u5bf9\u8c61\u7684GetOrdinal()\u65b9\u6cd5\uff0c\u6b64\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u5217\u540d\u5e76\u8fd4\u56de\u6b64\u5217\u540d\u6240\u5728\u7684\u5217\u53f7\u3002\u4f8b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n   int id=reader.GetOrdinal(&quot;CategoryName&quot;); \r\n   while(reader.Read()) \r\n   { \r\n   Response.Write(reader&#x5B;id]); \r\n   } \r\n   reader.Close();\r\n\r\n<\/pre>\n<p>\u81f3\u4e8e\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u5f88\u76f4\u89c2\uff0c\u4f8b\uff1a <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\u3000while(reader.Read())    \r\n        { Response.Write(reader.GetInt32(0).ToString()+&quot; &quot;+reader.GetString(1).ToString()+&quot; &lt;br&gt;&quot;);}\r\n\r\n<\/pre>\n<p>DataReader\u7684GetInt32()\u548cGetString()\u901a\u8fc7\u63a5\u6536\u4e00\u4e2a\u5217\u53f7\u6765\u8fd4\u56de\u4e00\u4e2a\u5217\u7684\u503c\uff0c\u8fd9\u4e24\u79cd\u662f\u6700\u5e38\u7528\u7684\uff0c\u5176\u4e2d\u3000\u3000\u8fd8\u6709\u5f88\u591a\u5176\u5b83\u7684\u7c7b\u578b\u3002      (\u6ce8\uff1aDataReader\u5bf9\u8c61\u5728\u8c03\u7528Close()\u65b9\u6cd5\u5373\u5173\u95ed\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\uff0c\u5982\u679c\u5728\u6ca1\u6709\u5173\u95ed\u4e4b\u524d\u53c8\u91cd\u65b0\u6253\u5f00\u7b2c\u4e8c\u4e2a\u8fde\u63a5\uff0c\u5219\u4f1a\u4ea7\u751f\u4e00\u6761\u5f02\u5e38\u4fe1\u606f) <\/p>\n<p>2.,ExecuteNonQuery() \u8fd9\u4e2a\u65b9\u6cd5\u5e76\u4e0d\u8fd4\u56de\u4e00\u4e2aDataReader\u5bf9\u8c61\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2aint\u7c7b\u578b\u7684\u503c\uff0c\u5373\u5728\u6267\u884c\u4e4b\u540e\u5728\u6570\u636e\u5e93\u4e2d\u6240\u5f71\u54cd\u7684\u884c\u6570\u3002<br \/>\n\u4f8b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nint affectrows=cmd.ExecuteNonQuery();    \r\n    Response.Write(affectrows +&quot; \u6761\u8bb0\u5f55\u53d7\u5f71\u54cd&quot;);\r\n\r\n<\/pre>\n<p>3,ExecuteScalar() \u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff0c\u4ec5\u4ec5\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u96c6\u4e2d\u7684\u7b2c\u4e00\u884c\u7b2c\u4e00\u5217\uff0c\u800c\u5ffd\u7565\u4e86\u5176\u5b83\u7684\u884c\u548c\u5217\uff0c\u800c\u4e14\u8fd4\u56de\u7684\u662f\u4e00\u4e2aobject\u7c7b\u578b\uff0c\u5728\u4f7f\u7528\u4e4b\u524d\u5fc5\u987b\u5148\u5c06\u5b83\u5f3a\u5236\u8f6c\u6362\u4e3a\u6240\u9700\u7c7b\u578b\u3002\u5982\u679c\u8fd4\u56de\u7684\u4ec5\u4ec5\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u6570\u636e\u5143\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u6b64\u65b9\u6cd5\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u3002\u4f8b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n   string strCon=&quot;server=localhost;database=Northwind;Trusted_Connection=Yes;&quot;; \r\n   string strqry=&quot;select count(*) from Categories&quot;; \r\n   SqlConnection con=new SqlConnection(strCon); \r\n   con.Open(); \r\n   SqlCommand cmd=con.CreateCommand(); \r\n   int i=Convert.ToInt32(cmd.ExecuteScalar());\u3000\/\/\u5fc5\u987b\u5f3a\u5236\u8f6c\u6362\r\n\r\n<\/pre>\n<p>4,ExecuteXmlReader() \u6b64\u65b9\u6cd5\u7528\u4e8e\uff38\uff2d\uff2c\u64cd\u4f5c\uff0c\u8fd4\u56de\u4e00\u4e2aXmlReader\u5bf9\u8c61\uff0c\u7531\u4e8e\u7cfb\u7edf\u9ed8\u8ba4\u6ca1\u6709\u5f15\u7528 System.Xml\u540d\u7a7a\u95f4\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u524d\u5f15\u5165\u3002\u4f8b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n   string strCon=&quot;server=localhost;database=Northwind;Trusted_Connection=Yes;&quot;; \r\n   SqlConnection con=new SqlConnection(strCon); \r\n   con.Open(); \r\n   SqlCommand cmd = new SqlCommand(&quot;select * from Categories FOR XML AUTO, XMLDATA&quot;, con); \r\n   XmlReader xr=cmd.ExecuteXmlReader(); \r\n   Response.Write(xr.AttributeCount);\u3000\u3000\/\/\u8fd9\u91cc\u83b7\u53d6\u5f53\u524d\u8282\u70b9\u4e0a\u7684\u5c5e\u6027\u4e2a\u6570 \r\n   \r\n   xr.Close(); \r\n   \r\n<\/pre>\n<p>\u6267\u884c\u5b8c\u6bd5\u4e4b\u540e\uff0c\u7167\u6837\u8981\u663e\u5f0f\u5730\u8c03\u7528Close()\u65b9\u6cd5\uff0c\u5426\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\u3002 <\/p>\n<p>\u4f7f\u7528\u53c2\u6570\u5316\u7684\u67e5\u8be2 <\/p>\n<p>\u5148\u770b\u4e00\u6bb5\uff33\uff31\uff2c\u8bed\u53e5\uff1aselect CategoryID,Description from Categories where CategoryID=? \u5176\u4e2d\u7684\u95ee\u53f7\u5c31\u662f\u4e00\u4e2a\u53c2\u6570\u3002\u4f46\u5728\u4f7f\u7528\u7684\u65f6\u5019\u5fc5\u987b\u662f\u5e26\u6709\uff20\u524d\u7f00\u7684\u547d\u540d\u53c2\u6570\uff0c\u56e0\u4e3a.NET\u6570\u636e\u63d0\u4f9b\u7a0b\u5e8f\u4e0d\u652f\u6301\u8fd9\u4e2a\u901a\u7528\u7684\u53c2\u6570\u6807\u8bb0\u201c\uff1f\u201d.\u4f7f\u7528\u53c2\u6570\u5316\u7684\u67e5\u8be2\u53ef\u4ee5\u5927\u5927\u5730\u7b80\u5316\u7f16\u7a0b\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u4e5f\u6bd4\u76f4\u63a5\u67e5\u8be2\u5b57\u7b26\u4e32\u8981\u9ad8\uff0c\u4e5f\u66f4\u65b9\u4fbf\uff0c\u5f88\u591a\u60c5\u51b5\u4e0b\u90fd\u9700\u8981\u66f4\u6539\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5c31\u63d0\u4f9b\u4e86\u65b9\u4fbf\uff0c\u53ea\u9700\u66f4\u6539\u53c2\u6570\u7684\u503c\u5373\u53ef\u3002\u4f8b\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n   string strCon=&quot;server=localhost;database=Northwind;Trusted_Connection=Yes;&quot;; \r\n   SqlConnection con=new SqlConnection(strCon); \r\n   con.Open(); \r\n   string strqry=&quot;select * from Categories where CategoryID=@CategoryID&quot;;\u3000\/\/\u5e26\u53c2\u6570\u7684\u67e5\u8be2 \r\n   SqlCommand cmd=new SqlCommand(strqry,con); \r\n   cmd.Parameters.Add(&quot;@CategoryID&quot;,SqlDbType.Int,4);\u3000\/\/\u7ed9\u53c2\u6570\u8d4b\u4e8e\u540c\u6570\u636e\u5e93\u4e2d\u76f8\u540c\u7684\u7c7b\u578b \r\n   cmd.Parameters&#x5B;&quot;@CategoryID&quot;].Value=&quot;3&quot;;\u3000\/\/\u7ed9\u53c2\u6570\u8d4b\u503c\uff0c\u53ef\u7075\u6d3b\u6539\u53d8 \r\n   SqlDataReader r=cmd.ExecuteReader();\u3000 \r\n   while(r.Read()) \r\n   { \r\n   Response.Write(r.GetString(2)+&quot;&lt;br&gt;&quot;);\u3000\/\/\u53d6\u51fa\u6307\u5b9a\u53c2\u6570\u5217\u7684\u503c \r\n   } \r\n   con.Close();\u3000\/\/\u5207\u8bb0\u5173\u95ed  \r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ADO.NET\u63d0\u4f9b\u4e86Connection\u6765\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u540c\u65f6\u4e5f\u63d0\u4f9b\u4e86Command\u5bf9\u8c61\u6765\u67e5\u8be2\u6570\u636e\u5e93\u3002\u540cConne [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-537","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=537"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/537\/revisions"}],"predecessor-version":[{"id":4626,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/537\/revisions\/4626"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}