{"id":8959,"date":"2024-10-08T10:30:38","date_gmt":"2024-10-08T09:30:38","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=8959"},"modified":"2024-10-08T10:31:47","modified_gmt":"2024-10-08T09:31:47","slug":"mysql-load-blob-as-stream-to-file-async","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/mysql-load-blob-as-stream-to-file-async\/","title":{"rendered":"MySQL load BLOB as stream to file (async)"},"content":{"rendered":"\n<p>See also: <a href=\"https:\/\/solidt.eu\/site\/c-download-file-from-database-as-stream\/\" data-type=\"link\" data-id=\"https:\/\/solidt.eu\/site\/c-download-file-from-database-as-stream\/\">https:\/\/solidt.eu\/site\/c-download-file-from-database-as-stream\/<\/a><\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">using MySql.Data.MySqlClient; \/\/ Zorg ervoor dat MySqlConnector is ge\u00efnstalleerd (MySql.Data)\nusing System.IO;\nusing System.Threading.Tasks;\n\nclass Program\n{\n    static async Task Main(string[] args)\n    {\n        string connectionString = \"Server=your_server;Database=your_database;User ID=your_user;Password=your_password;\";\n        string query = \"SELECT blob_column FROM your_table WHERE id = @Id\";\n        int id = 1; \/\/ Stel dit in op de id-waarde van het record dat je wilt ophalen\n\n        await ReadBlobToFileUsingStreamAsync(connectionString, query, id, \"output_file_path\");\n    }\n\n    static async Task ReadBlobToFileUsingStreamAsync(string connectionString, string query, int id, string filePath)\n    {\n        using (var connection = new MySqlConnection(connectionString))\n        {\n            await connection.OpenAsync();\n            using (var command = new MySqlCommand(query, connection))\n            {\n                command.Parameters.AddWithValue(\"@Id\", id);\n\n                using (var reader = await command.ExecuteReaderAsync(System.Data.CommandBehavior.SequentialAccess)) \n                {\n                    if (await reader.ReadAsync())\n                    {\n                        \/\/ Haal de index van de kolom op, in dit voorbeeld is de kolomnaam \"blob_column\"\n                        int blobColumnIndex = reader.GetOrdinal(\"blob_column\");\n\n                        \/\/ Verkrijg een stream van de BLOB-kolom\n                        using (Stream blobStream = reader.GetStream(blobColumnIndex))\n                        using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))\n                        {\n                            \/\/ Kopieer de BLOB-stream asynchroon naar de FileStream\n                            await blobStream.CopyToAsync(fileStream);\n                            Console.WriteLine($\"BLOB-gegevens succesvol geschreven naar {filePath}.\");\n                        }\n                    }\n                    else\n                    {\n                        Console.WriteLine(\"Geen gegevens gevonden voor het opgegeven ID.\");\n                    }\n                }\n            }\n        }\n    }\n}\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>See also: https:\/\/solidt.eu\/site\/c-download-file-from-database-as-stream\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8959","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=8959"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8959\/revisions"}],"predecessor-version":[{"id":8962,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8959\/revisions\/8962"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=8959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=8959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=8959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}