I read up on inserting images into SQL server databases but I couldn't make my code work. Here is my initial code.
public void InsertResults(string Id, Bitmap bitmap)
{
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO Results VALUES(@ResultID, @HasSucceeded, @ScenarioID, @Screenshot)", conn))
{
cmd.Parameters.AddWithValue("@ResultID", 0);
cmd.Parameters.AddWithValue("@HasSucceeded", 0);
cmd.Parameters.AddWithValue("@ScenarioID", Id);
cmd.Parameters.AddWithValue("@Screenshot", bitmap);
cmd.ExecuteNonQuery();
}
}
}
catch (SqlException ex)
{
//Log exception
}
}
I know that you can't just insert a Bitmap image like I tried to above. How do I go about converting the Bitmap into something I can insert?
I know some SQL like this below has to be used but I'm not sure how to integrate it into my code.
INSERT INTO DatabaseImageTable ([image name], [image])
SELECT 'SQL Server Image', *
FROM OPENROWSET(BULK N'C:\images\sql-server-image-file.jpg', SINGLE_BLOB) image;