{"id":96908,"date":"2024-10-20T13:22:19","date_gmt":"2024-10-20T05:22:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96908\/"},"modified":"2024-10-20T13:22:20","modified_gmt":"2024-10-20T05:22:20","slug":"postgresql%e7%9a%84-net%e9%a9%85%e5%8b%95%e7%a8%8b%e5%ba%8fnpgsql%e4%b8%ad%e5%8f%83%e6%95%b8%e5%b0%8d%e8%b1%a1%e7%9a%84%e4%b8%80%e5%80%8bbug","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96908\/","title":{"rendered":"PostgreSQL\u7684.NET\u9a45\u52d5\u7a0b\u5e8fNpgsql\u4e2d\u53c3\u6578\u5c0d\u8c61\u7684\u4e00\u500bBug"},"content":{"rendered":"<h1 id=\"postgresql%e7%9a%84-net%e9%a9%85%e5%8b%95%e7%a8%8b%e5%ba%8fnpgsql%e4%b8%ad%e5%8f%83%e6%95%b8%e5%b0%8d%e8%b1%a1%e7%9a%84%e4%b8%80%e5%80%8bbug-JHBFMPFyMD\">PostgreSQL\u7684.NET\u9a45\u52d5\u7a0b\u5e8fNpgsql\u4e2d\u53c3\u6578\u5c0d\u8c61\u7684\u4e00\u500bBug<\/h1>\n<p>\u5728\u4f7f\u7528PostgreSQL\u6578\u64da\u5eab\u6642\uff0c.NET\u958b\u767c\u8005\u7d93\u5e38\u9078\u64c7Npgsql\u4f5c\u70ba\u5176\u6578\u64da\u8a2a\u554f\u7684\u9a45\u52d5\u7a0b\u5e8f\u3002Npgsql\u662f\u4e00\u500b\u958b\u6e90\u7684.NET\u6578\u64da\u63d0\u4f9b\u7a0b\u5e8f\uff0c\u5c08\u70baPostgreSQL\u8a2d\u8a08\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6578\u64da\u64cd\u4f5c\u80fd\u529b\u3002\u7136\u800c\uff0c\u6700\u8fd1\u5728\u4f7f\u7528Npgsql\u6642\u767c\u73fe\u4e86\u4e00\u500b\u8207\u53c3\u6578\u5c0d\u8c61\u76f8\u95dc\u7684Bug\uff0c\u9019\u53ef\u80fd\u6703\u5f71\u97ff\u958b\u767c\u8005\u7684\u5de5\u4f5c\u6d41\u7a0b\u548c\u61c9\u7528\u7a0b\u5e8f\u7684\u7a69\u5b9a\u6027\u3002<\/p>\n<h2 id=\"npgsql%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-JHBFMPFyMD\">Npgsql\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Npgsql\u5141\u8a31.NET\u61c9\u7528\u7a0b\u5e8f\u8207PostgreSQL\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u5b83\u652f\u6301\u591a\u7a2e\u6578\u64da\u985e\u578b\uff0c\u4e26\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684API\u4f86\u57f7\u884c\u67e5\u8a62\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u64cd\u4f5c\u3002Npgsql\u7684\u8a2d\u8a08\u65e8\u5728\u7c21\u5316\u8207PostgreSQL\u7684\u4ea4\u4e92\uff0c\u4e26\u63d0\u4f9b\u826f\u597d\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e5%8f%83%e6%95%b8%e5%b0%8d%e8%b1%a1%e7%9a%84%e4%bd%bf%e7%94%a8-JHBFMPFyMD\">\u53c3\u6578\u5c0d\u8c61\u7684\u4f7f\u7528<\/h2>\n<p>\u5728Npgsql\u4e2d\uff0c\u53c3\u6578\u5c0d\u8c61\u901a\u5e38\u7528\u65bc\u5b89\u5168\u5730\u50b3\u905e\u6578\u64da\u5230SQL\u67e5\u8a62\u4e2d\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6027\u80fd\uff0c\u9084\u80fd\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u53c3\u6578\u5c0d\u8c61\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>using (var conn = new NpgsqlConnection(\"Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase\"))\n{\n    conn.Open();\n    using (var cmd = new NpgsqlCommand(\"SELECT * FROM mytable WHERE id = @id\", conn))\n    {\n        cmd.Parameters.AddWithValue(\"id\", 1);\n        using (var reader = cmd.ExecuteReader())\n        {\n            while (reader.Read())\n            {\n                Console.WriteLine(reader.GetString(0));\n            }\n        }\n    }\n}<\/code><\/pre>\n<h2 id=\"bug%e7%9a%84%e6%8f%8f%e8%bf%b0-JHBFMPFyMD\">Bug\u7684\u63cf\u8ff0<\/h2>\n<p>\u6700\u8fd1\uff0c\u6709\u958b\u767c\u8005\u5831\u544a\u5728\u4f7f\u7528Npgsql\u7684\u53c3\u6578\u5c0d\u8c61\u6642\u51fa\u73fe\u4e86\u4e00\u500bBug\u3002\u5177\u9ad4\u4f86\u8aaa\uff0c\u7576\u4f7f\u7528\u67d0\u4e9b\u6578\u64da\u985e\u578b\uff08\u5982JSON\u6216JSONB\uff09\u4f5c\u70ba\u53c3\u6578\u6642\uff0cNpgsql\u672a\u80fd\u6b63\u78ba\u8655\u7406\u9019\u4e9b\u6578\u64da\uff0c\u5c0e\u81f4\u67e5\u8a62\u5931\u6557\u6216\u8fd4\u56de\u4e0d\u6b63\u78ba\u7684\u7d50\u679c\u3002\u9019\u500b\u554f\u984c\u4e3b\u8981\u51fa\u73fe\u5728\u4f7f\u7528<code>cmd.Parameters.AddWithValue<\/code>\u65b9\u6cd5\u6642\uff0c\u7576\u50b3\u905e\u7684\u5c0d\u8c61\u985e\u578b\u4e0d\u7b26\u5408\u9810\u671f\u6642\uff0cNpgsql\u7121\u6cd5\u6b63\u78ba\u89e3\u6790\u9019\u4e9b\u53c3\u6578\u3002<\/p>\n<h2 id=\"bug%e7%9a%84%e5%bd%b1%e9%9f%bf-JHBFMPFyMD\">Bug\u7684\u5f71\u97ff<\/h2>\n<p>\u9019\u500bBug\u53ef\u80fd\u6703\u5c0d\u958b\u767c\u8005\u9020\u6210\u56f0\u64fe\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u8907\u96dc\u6578\u64da\u7d50\u69cb\u6642\u3002\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<ul>\n<li>\u67e5\u8a62\u57f7\u884c\u5931\u6557\uff0c\u4e26\u8fd4\u56de\u7570\u5e38\u4fe1\u606f\u3002<\/li>\n<li>\u67e5\u8a62\u8fd4\u56de\u7684\u7d50\u679c\u4e0d\u6b63\u78ba\uff0c\u5c0e\u81f4\u6578\u64da\u4e0d\u4e00\u81f4\u3002<\/li>\n<li>\u589e\u52a0\u4e86\u8abf\u8a66\u7684\u96e3\u5ea6\uff0c\u56e0\u70ba\u932f\u8aa4\u4fe1\u606f\u53ef\u80fd\u4e0d\u5920\u660e\u78ba\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-JHBFMPFyMD\">\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u70ba\u4e86\u907f\u514d\u9019\u500bBug\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5e7e\u7a2e\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>NpgsqlParameter<\/code>\u985e\u4f86\u660e\u78ba\u6307\u5b9a\u53c3\u6578\u7684\u985e\u578b\uff0c\u800c\u4e0d\u662f\u4f7f\u7528<code>AddWithValue<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/li>\n<pre><code>var param = new NpgsqlParameter(\"id\", NpgsqlTypes.NpgsqlDbType.Jsonb) { Value = jsonData };\ncmd.Parameters.Add(param);<\/code><\/pre>\n<li>\u6aa2\u67e5\u50b3\u905e\u7684\u6578\u64da\u985e\u578b\uff0c\u78ba\u4fdd\u5176\u8207PostgreSQL\u6578\u64da\u5eab\u4e2d\u7684\u985e\u578b\u76f8\u5339\u914d\u3002<\/li>\n<li>\u5b9a\u671f\u66f4\u65b0Npgsql\u5230\u6700\u65b0\u7248\u672c\uff0c\u4ee5\u7372\u53d6Bug\u4fee\u5fa9\u548c\u6027\u80fd\u6539\u9032\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-JHBFMPFyMD\">\u7d50\u8ad6<\/h2>\n<p>\u5728\u4f7f\u7528Npgsql\u9032\u884cPostgreSQL\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u4e86\u89e3\u548c\u89e3\u6c7a\u53c3\u6578\u5c0d\u8c61\u4e2d\u7684Bug\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u904e\u6b63\u78ba\u4f7f\u7528\u53c3\u6578\u548c\u5b9a\u671f\u66f4\u65b0\u9a45\u52d5\u7a0b\u5e8f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6e1b\u5c11\u6f5b\u5728\u7684\u554f\u984c\uff0c\u78ba\u4fdd\u61c9\u7528\u7a0b\u5e8f\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<code>VPS<\/code>\u548c<code>\u9999\u6e2f\u4f3a\u670d\u5668<\/code>\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9 <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u8a0ePostgreSQL\u7684.NET\u9a45\u52d5\u7a0b\u5e8fNpgsql\u4e2d\u7684\u4e00\u500b\u53c3\u6578\u5c0d\u8c61Bug\uff0c\u5f71\u97ff\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u7a69\u5b9a\u6027\u8207\u6027\u80fd\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-96908","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96908","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=96908"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96908\/revisions"}],"predecessor-version":[{"id":96909,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96908\/revisions\/96909"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}