{"id":5273,"date":"2024-11-14T11:24:58","date_gmt":"2024-11-14T03:24:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5273\/"},"modified":"2024-11-14T11:24:58","modified_gmt":"2024-11-14T03:24:58","slug":"mysql%e4%b8%adblob%e6%80%8e%e4%b9%88%e8%af%bb%e5%8f%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5273\/","title":{"rendered":"mysql\u4e2dblob\u600e\u4e48\u8bfb\u53d6"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7\u4f7f\u7528 sql \u67e5\u8be2\uff081\uff09\u3001\u83b7\u53d6\u6570\u636e\u8bfb\u53d6\u5668\uff082\uff09\u3001\u4f7f\u7528 getblob \u65b9\u6cd5\uff083\uff09\u8bfb\u53d6\u6570\u636e\uff0c\u53ef\u4ee5\u4ece mysql \u4e2d\u8bfb\u53d6 blob \u7c7b\u578b\u6570\u636e\uff0c\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f8b\u5982\u56fe\u50cf\u6216\u6587\u4ef6\uff084\u30015\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052618071342028.jpg\" class=\"aligncenter\" title=\"mysql\u4e2dblob\u600e\u4e48\u8bfb\u53d6\u63d2\u56fe\" alt=\"mysql\u4e2dblob\u600e\u4e48\u8bfb\u53d6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 MySQL \u4e2d\u8bfb\u53d6 BLOB \u7c7b\u578b\u6570\u636e<\/strong><\/p>\n<p>BLOB\uff08\u4e8c\u8fdb\u5236\u5927\u578b\u5bf9\u8c61\uff09\u7c7b\u578b\u7684\u6570\u636e\u5728 MySQL \u4e2d\u7528\u4e8e\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f8b\u5982\u56fe\u50cf\u3001\u6587\u4ef6\u6216\u89c6\u9891\u3002\u8981\u8bfb\u53d6 BLOB \u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/p>\n<p>\u4f7f\u7528\u9002\u5f53\u7684\u8fde\u63a5\u65b9\u6cd5\uff08\u4f8b\u5982 MySQLConnector \u6216 ADO.NET\uff09\u5efa\u7acb\u4e0e MySQL \u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002<\/p>\n<p><strong>2. \u6267\u884c\u67e5\u8be2<\/strong><\/p>\n<p>\u7f16\u5199\u4e00\u4e2a SQL \u67e5\u8be2\u4ee5\u9009\u62e9\u5305\u542b BLOB \u6570\u636e\u7684\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>SELECT image_data FROM products WHERE product_id = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong><\/p>\n<p>\u6267\u884c\u67e5\u8be2\u540e\uff0c\u5c06\u83b7\u5f97\u4e00\u4e2a\u5305\u542b\u67e5\u8be2\u7ed3\u679c\u7684\u6570\u636e\u8bfb\u53d6\u5668\u6216\u7ed3\u679c\u96c6\u5bf9\u8c61\u3002\u627e\u5230\u5305\u542b BLOB \u6570\u636e\u7684\u5217\u7d22\u5f15\u3002<\/p>\n<p><strong>4. \u8bfb\u53d6 BLOB \u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528 GetBlob \u65b9\u6cd5\u8bfb\u53d6 BLOB \u6570\u636e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b BLOB \u4e8c\u8fdb\u5236\u6570\u636e\u7684 MemoryStream \u5bf9\u8c61\u6216\u7c7b\u4f3c\u7684\u6d41\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>using System.Data.SqlClient;\n\n...\n\n\/\/ Get the BLOB data using GetBlob method\nbyte[] imageBytes = reader.GetBlob(reader.GetOrdinal(\"image_data\"));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u5904\u7406 BLOB \u6570\u636e<\/strong><\/p>\n<p>\u5904\u7406 BLOB \u6570\u636e\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u4f7f\u7528\u76ee\u7684\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4e4b\u4e00\uff1a<\/p>\n<ul>\n<li>\u5c06 MemoryStream \u5bf9\u8c61\u4f20\u9012\u7ed9\u80fd\u591f\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u7ec4\u4ef6\uff08\u4f8b\u5982\u56fe\u50cf\u67e5\u770b\u5668\u6216\u6587\u4ef6\u7cfb\u7edf\uff09<\/li>\n<li>\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff08\u4f8b\u5982 base64 \u7f16\u7801\uff09\u4ee5\u8fdb\u884c\u7f51\u7edc\u4f20\u8f93\u6216\u5b58\u50a8<\/li>\n<li>\u5728\u6d41\u5bf9\u8c61\u4e0a\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u4f8b\u5982\u590d\u5236\u3001\u5199\u5165\u6216\u641c\u7d22<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece MySQL \u8bfb\u53d6 BLOB \u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e2dblob\u600e\u4e48\u8bfb\u53d6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u4f7f\u7528 sql \u67e5\u8be2\uff081\uff09\u3001\u83b7\u53d6\u6570\u636e\u8bfb\u53d6\u5668\uff082\uff09\u3001\u4f7f\u7528 getblob \u65b9\u6cd5\uff083\uff09\u8bfb\u53d6\u6570\u636e\uff0c\u53ef\u4ee5\u4ece mysql \u4e2d\u8bfb\u53d6 blob \u7c7b\u578b\u6570\u636e\uff0c\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f8b\u5982\u56fe\u50cf\u6216\u6587\u4ef6\uff084\u30015\uff09\u3002 \u5982\u4f55\u5728 MySQL \u4e2d\u8bfb\u53d6 BLOB \u7c7b\u578b\u6570\u636e BLOB\uff08\u4e8c\u8fdb\u5236\u5927\u578b\u5bf9\u8c61\uff09\u7c7b\u578b\u7684\u6570\u636e\u5728 MySQL \u4e2d\u7528\u4e8e\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f8b\u5982\u56fe\u50cf\u3001\u6587\u4ef6\u6216\u89c6\u9891\u3002\u8981\u8bfb\u53d6 BLOB \u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5 \u4f7f\u7528\u9002\u5f53\u7684\u8fde\u63a5\u65b9\u6cd5\uff08\u4f8b\u5982 MySQLConnector \u6216 ADO.NET\uff09\u5efa\u7acb\u4e0e MySQL \u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002 2. \u6267\u884c\u67e5\u8be2 \u7f16\u5199\u4e00\u4e2a SQL \u67e5\u8be2\u4ee5\u9009\u62e9\u5305\u542b BLOB \u6570\u636e\u7684\u5217\u3002\u4f8b\u5982\uff1a SELECT image_data FROM products WHERE product_id = 1; \u767b\u5f55\u540e\u590d\u5236 3. \u83b7\u53d6\u67e5\u8be2\u7ed3\u679c \u6267\u884c\u67e5\u8be2\u540e\uff0c\u5c06\u83b7\u5f97\u4e00\u4e2a\u5305\u542b\u67e5\u8be2\u7ed3\u679c\u7684\u6570\u636e\u8bfb\u53d6\u5668\u6216\u7ed3\u679c\u96c6\u5bf9\u8c61\u3002\u627e\u5230\u5305\u542b BLOB \u6570\u636e\u7684\u5217\u7d22\u5f15\u3002 4. \u8bfb\u53d6 BLOB \u6570\u636e \u4f7f\u7528 GetBlob \u65b9\u6cd5\u8bfb\u53d6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5273","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5273"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5273\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}