Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The 'Parameter is not valid' error is occurring because the byte array you've fetched back from the database does not represent valid image binary data.</p> <p>There are various reasons this might happen. As a start, take a look at the data in your database - it might not be storing the binary data you expect.</p> <p>In the meantime, I've quickly thrown together a small console app that does what you're after:</p> <pre><code>class LoadImageFromDbSpike { private const string ConnectionString = @"Data Source=[SERVERNAME];Initial Catalog=[DBNAME];Trusted_Connection=true;Connect Timeout=180;"; private const string ImageOnDiskPath = @"c:\test.png"; private const string OutputPath = @"c:\output.png"; static void Main(string[] args) { var imageData = File.ReadAllBytes(ImageOnDiskPath); var imageId = StoreImageData(imageData); var imageDataFromDb = LoadImageData(imageId); File.WriteAllBytes(OutputPath, imageDataFromDb); } private static int StoreImageData(IEnumerable&lt;byte&gt; imageData) { const string insertCommand = "INSERT INTO ImageSpike (Image) " + "VALUES (@ImageData); " + "SELECT SCOPE_IDENTITY();"; using (var con = new SqlConnection(ConnectionString)) using (var cmd = new SqlCommand(insertCommand, con)) { cmd.Parameters.AddWithValue("@ImageData", imageData); cmd.Connection.Open(); return (int) (decimal) cmd.ExecuteScalar(); } } private static byte[] LoadImageData(int id) { const string loadImageCommand = "SELECT TOP 1 Image FROM ImageSpike " + "WHERE Id = @Id; "; using (var con = new SqlConnection(ConnectionString)) using (var cmd = new SqlCommand(loadImageCommand, con)) { cmd.Parameters.AddWithValue("@Id", id); cmd.Connection.Open(); var result = cmd.ExecuteScalar(); return (byte[]) result; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload