Varbinary(max) insertion - duplication of varbinary(max) fields between unrelated db records

85 views Asked by At

I'm trying to rough out a concept here and I have an odd problem. For your amusement...

In summary:

What I want to do is generate an image in C# code, insert it into a varbinary(max) field of a SQL 2012 db, and then display it in an SSRS report. (Yes, I have remembered to delete the rdl.data cache after every test.) The image is simply a circle with a highlighted arc segment. This db table is based on the one in http://www.kodyaz.com/articles/display-database-image-using-sql-server-2008-reporting-services.aspx The only thing I've changed is to add a primary key. I've created a DataSet (xsd) file and dragged the table onto it from the Server Explorer.

The weird part is that, although I'm inserting records successfully and the fname fields is being inserted correctly, as far as I can tell, the binary field is always a duplicate of the first one added. I should also mention when I initially tested the main graphics code by writing to a local disk file, the image always came out correctly.

At first I thought it might be a problem doing it from a web service I have but I get the exact same behavior running the code in a local test web app. Also all the code is local to the method. Nothing remotely persistent. Admittedly I only dabble in graphics. This is hacked together from various other references. Here's the heart of the code. I have it in a try/catch and it's not giving any exceptions.

Bitmap x;
Pen p = new Pen(System.Drawing.Color.Red, 5);
Pen p2 = new Pen(System.Drawing.Color.Pink, 5);
SolidBrush b;
RectangleF rf;
Graphics gr;       
p = new Pen(System.Drawing.Color.Red, iThick);
p2 = new Pen(System.Drawing.Color.Pink, iThick);
x = new Bitmap(500, 500);
rf = new RectangleF(0, 0, x.Width, x.Height);
gr = Graphics.FromImage(x);
b = new SolidBrush(System.Drawing.Color.White);
gr.FillRectangle(b, rf);
rf = new RectangleF(p.Width, p.Width, x.Width - p.Width * 2, x.Height - p.Width * 2);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.DrawEllipse(p2, rf);
gr.DrawArc(p, rf, fStartAngle, fSweepAngle);
byte[] bytes;
using (System.IO.MemoryStream sampleStream = new System.IO.MemoryStream())
{                
      x.Save(sampleStream, System.Drawing.Imaging.ImageFormat.Bmp);                
      bytes = sampleStream.ToArray();
      dsCountryTableAdapters.DBFiles1TableAdapter ta = new dsCountryTableAdapters.DBFiles1TableAdapter();
      ta.Insert(sImageName, bytes);
}
1

There are 1 answers

0
user2996466 On

Whatever the problem originally was, changing the insert method from a TableAdapter.Insert to a SQLConnection/SQLCommand setup cured it. (No change to the data table structure.) Still curious why the TA didn't work.

using (SqlCommand cmd = new SqlCommand("INSERT INTO DBFiles(fname, [file]) VALUES (@fname, @file)", conn))
            {
                using (System.IO.MemoryStream sampleStream = new System.IO.MemoryStream())
                {
                    x.Save(sampleStream, System.Drawing.Imaging.ImageFormat.Bmp);
                    bytes = sampleStream.ToArray();
                }                
                cmd.Parameters.Add("@fname", SqlDbType.NVarChar, 1000).Value = sImageName;
                cmd.Parameters.Add("@file", SqlDbType.VarBinary, -1).Value = bytes;
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }