{"id":58039,"date":"2024-10-12T19:15:19","date_gmt":"2024-10-12T11:15:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58039\/"},"modified":"2024-10-12T19:15:19","modified_gmt":"2024-10-12T11:15:19","slug":"net%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%aemysql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-net-mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58039\/","title":{"rendered":".NET\u7a0b\u5e8f\u5982\u4f55\u914d\u7f6eMySQL\u6578\u64da\u5eab\uff1f (.net mysql\u6578\u64da\u5eab\u914d\u7f6e\u6587\u4ef6)"},"content":{"rendered":"<h1 id=\"net%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%aemysql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-net-mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6-iFlVUkGbRn\">.NET\u7a0b\u5e8f\u5982\u4f55\u914d\u7f6eMySQL\u6578\u64da\u5eab\uff1f (.net mysql\u6578\u64da\u5eab\u914d\u7f6e\u6587\u4ef6)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u4e2d\uff0c.NET\u6846\u67b6\u8207MySQL\u6578\u64da\u5eab\u7684\u7d50\u5408\u8d8a\u4f86\u8d8a\u666e\u904d\u3002\u9019\u7a2e\u7d44\u5408\u4e0d\u50c5\u80fd\u5920\u63d0\u4f9b\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\uff0c\u9084\u80fd\u5920\u652f\u6301\u591a\u7a2e\u61c9\u7528\u7a0b\u5e8f\u7684\u958b\u767c\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728.NET\u7a0b\u5e8f\u4e2d\u914d\u7f6eMySQL\u6578\u64da\u5eab\uff0c\u5305\u62ec\u5fc5\u8981\u7684\u6b65\u9a5f\u548c\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"1-%e5%ae%89%e8%a3%9dmysql%e6%95%b8%e6%93%9a%e5%ba%ab-iFlVUkGbRn\">1. \u5b89\u88ddMySQL\u6578\u64da\u5eab<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u60a8\u7684\u4f3a\u670d\u5668\u4e0a\u5b89\u88ddMySQL\u6578\u64da\u5eab\u3002\u53ef\u4ee5\u5f9e<a href=\"https:\/\/dev.mysql.com\/downloads\/mysql\/\" rel=\"nofollow noopener\" target=\"_blank\">MySQL\u5b98\u65b9\u7db2\u7ad9<\/a>\u4e0b\u8f09\u5b89\u88dd\u5305\uff0c\u4e26\u6839\u64da\u6307\u793a\u5b8c\u6210\u5b89\u88dd\u3002\u5b89\u88dd\u904e\u7a0b\u4e2d\uff0c\u8acb\u8a18\u4f4f\u8a2d\u7f6eroot\u7528\u6236\u7684\u5bc6\u78bc\uff0c\u9019\u5c07\u5728\u5f8c\u7e8c\u7684\u914d\u7f6e\u4e2d\u4f7f\u7528\u3002<\/p>\n<h2 id=\"2-%e5%ae%89%e8%a3%9dmysql-connector-net-iFlVUkGbRn\">2. \u5b89\u88ddMySQL Connector\/NET<\/h2>\n<p>\u70ba\u4e86\u8b93.NET\u61c9\u7528\u7a0b\u5e8f\u80fd\u5920\u8207MySQL\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u60a8\u9700\u8981\u5b89\u88ddMySQL Connector\/NET\u3002\u9019\u662f\u4e00\u500b.NET\u6578\u64da\u63d0\u4f9b\u7a0b\u5e8f\uff0c\u5141\u8a31.NET\u61c9\u7528\u7a0b\u5e8f\u8207MySQL\u6578\u64da\u5eab\u9032\u884c\u901a\u4fe1\u3002\u60a8\u53ef\u4ee5\u901a\u904eNuGet\u5305\u7ba1\u7406\u5668\u5b89\u88dd\u5b83\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>Install-Package MySql.Data<\/code><\/pre>\n<h2 id=\"3-%e9%85%8d%e7%bd%ae%e9%80%a3%e6%8e%a5%e5%ad%97%e7%ac%a6%e4%b8%b2-iFlVUkGbRn\">3. \u914d\u7f6e\u9023\u63a5\u5b57\u7b26\u4e32<\/h2>\n<p>\u5728.NET\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u9023\u63a5\u5b57\u7b26\u4e32\u662f\u7528\u4f86\u6307\u5b9a\u5982\u4f55\u9023\u63a5\u5230\u6578\u64da\u5eab\u7684\u3002\u901a\u5e38\uff0c\u9023\u63a5\u5b57\u7b26\u4e32\u6703\u653e\u5728\u61c9\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4f8b\u5982Web.config\u6216App.config\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\u9023\u63a5\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>&lt;connectionStrings&gt;\n    &lt;add name=\"MySqlConnection\" \n         connectionString=\"Server=localhost;Database=mydatabase;User ID=root;Password=my_password;Pooling=true;\" \n         providerName=\"MySql.Data.MySqlClient\" \/&gt;\n&lt;\/connectionStrings&gt;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u60a8\u9700\u8981\u6839\u64da\u5be6\u969b\u60c5\u6cc1\u66ff\u63db\u4ee5\u4e0b\u53c3\u6578\uff1a<\/p>\n<ul>\n<li><strong>Server<\/strong>: MySQL\u4f3a\u670d\u5668\u7684\u5730\u5740\uff0c\u901a\u5e38\u662flocalhost\u3002<\/li>\n<li><strong>Database<\/strong>: \u60a8\u8981\u9023\u63a5\u7684\u6578\u64da\u5eab\u540d\u7a31\u3002<\/li>\n<li><strong>User ID<\/strong>: \u7528\u6236\u540d\uff0c\u901a\u5e38\u662froot\u3002<\/li>\n<li><strong>Password<\/strong>: \u60a8\u5728\u5b89\u88ddMySQL\u6642\u8a2d\u7f6e\u7684\u5bc6\u78bc\u3002<\/li>\n<\/ul>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8mysql%e6%95%b8%e6%93%9a%e5%ba%ab-iFlVUkGbRn\">4. \u4f7f\u7528MySQL\u6578\u64da\u5eab<\/h2>\n<p>\u914d\u7f6e\u597d\u9023\u63a5\u5b57\u7b26\u4e32\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5728\u4ee3\u78bc\u4e2d\u4f7f\u7528\u5b83\u4f86\u9023\u63a5MySQL\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528C#\u4f86\u67e5\u8a62\u6578\u64da\uff1a<\/p>\n<pre><code>using MySql.Data.MySqlClient;\n\nstring connectionString = ConfigurationManager.ConnectionStrings[\"MySqlConnection\"].ConnectionString;\n\nusing (MySqlConnection conn = new MySqlConnection(connectionString))\n{\n    conn.Open();\n    string query = \"SELECT * FROM users\";\n    MySqlCommand cmd = new MySqlCommand(query, conn);\n    MySqlDataReader reader = cmd.ExecuteReader();\n\n    while (reader.Read())\n    {\n        Console.WriteLine(reader[\"username\"].ToString());\n    }\n}<\/code><\/pre>\n<h2 id=\"5-%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86-iFlVUkGbRn\">5. \u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5728\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u6642\uff0c\u932f\u8aa4\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528try-catch\u584a\u4f86\u6355\u7372\u53ef\u80fd\u767c\u751f\u7684\u7570\u5e38\uff0c\u4e26\u9032\u884c\u76f8\u61c9\u7684\u8655\u7406\uff1a<\/p>\n<pre><code>try\n{\n    \/\/ \u9023\u63a5\u548c\u67e5\u8a62\u4ee3\u78bc\n}\ncatch (MySqlException ex)\n{\n    Console.WriteLine(\"\u6578\u64da\u5eab\u932f\u8aa4: \" + ex.Message);\n}\ncatch (Exception ex)\n{\n    Console.WriteLine(\"\u5176\u4ed6\u932f\u8aa4: \" + ex.Message);\n}<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-iFlVUkGbRn\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u6210\u529f\u5730\u5728.NET\u7a0b\u5e8f\u4e2d\u914d\u7f6eMySQL\u6578\u64da\u5eab\u3002\u9019\u4e0d\u50c5\u80fd\u5920\u63d0\u9ad8\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\uff0c\u9084\u80fd\u5920\u70ba\u7528\u6236\u63d0\u4f9b\u66f4\u597d\u7684\u9ad4\u9a57\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u4f3a\u670d\u5668\u4f86\u904b\u884c\u60a8\u7684.NET\u61c9\u7528\u7a0b\u5e8f\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>\u4e86\u89e3\u5982\u4f55\u5728.NET\u7a0b\u5e8f\u4e2d\u914d\u7f6eMySQL\u6570\u636e\u5e93\uff0c\u5305\u62ec\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\u548c\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u8be6\u7ec6\u6b65\u9aa4\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-58039","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58039","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=58039"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58039\/revisions"}],"predecessor-version":[{"id":58040,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58039\/revisions\/58040"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}