{"id":3010,"date":"2019-12-04T16:29:30","date_gmt":"2019-12-04T15:29:30","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3010"},"modified":"2022-07-25T08:33:24","modified_gmt":"2022-07-25T07:33:24","slug":"c-download-file-from-database-as-stream","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-download-file-from-database-as-stream\/","title":{"rendered":"C# Download file from MSSql database column as stream"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public async Task&lt;Stream> DownloadById(Guid workorderattachmentid)\n{\n\tusing (var connection = UnitOfWork.GetContext().Database.GetDbConnection()) {\n\t\tawait connection.OpenAsync();\n\t\n\t\tvar command = connection.CreateCommand();\n\t\tcommand.CommandText = \"SELECT Data FROM WorkOrderAttachment WHERE Id = @id\";\n\t\tvar param = command.CreateParameter();\n\t\tparam.ParameterName = \"@id\";\n\t\tparam.Value = workorderattachmentid.ToString();\n\t\tcommand.Parameters.Add(param);\n\n\t\tvar reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection);\n\t\tawait reader.ReadAsync(); \n\t\tif (await reader.IsDBNullAsync(0))\n\t\t\t\treturn null;\n\t\tvar stream = reader.GetStream(0);\n\t\treturn stream;\n\t}\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","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":[6,4,1],"tags":[],"class_list":["post-3010","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3010","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=3010"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3010\/revisions"}],"predecessor-version":[{"id":4038,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3010\/revisions\/4038"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}