0

I have a client who is asking me to import jpeg files into their SQL Server database.

The problem is that their database vendor originally build the product in SQL Server 2000 (or earlier) although their database currently sits on a SQL Server 2008 R2 instance. It appears that their old data looks like it's just being dumped into a big field of datatype text.

Is it possible to read the jpeg file into a MemoryStream then write that directly to the SQL Server database? If so, how would I go about doing this? Or, is there a better way?

Thanks,

Andre

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You can read this first: http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – 2power10 May 21 '13 at 01:49
  • Better way: convert that column to `VARBINARY(MAX)` - after all, a picture is **binary** data, not text...... – marc_s May 21 '13 at 04:35

2 Answers2

1

It is not cost effective nor efficient to store pictures in the DB your best approach is to store the path to the file.

HectorDZJ
  • 43
  • 5
  • Hector, While I happen to agree with you, and if I were the architect of a brand new solution I would certainly choose to link instead of embed. However my requirement is to work with a pre-built app and one where the raw contents of the jpeg file are written to a TEXT data type, instead of bulk-loaded COLUMNSTORE to a varchar(max) data type. That being said, is their any way in C# to write the raw contents of a unicode stream directly to database? – Andre Ranieri May 21 '13 at 03:22
0

I suppose if you really needed to do this, you can build the byte-stream into a string like this:

        MemoryStream ms = [your stream];
        Byte[] memoryBytes = ms.ToArray();
        StringBuilder sBuilder  = new StringBuilder();

        foreach(byte nextByte in memoryBytes)
            sBuilder.Append((char)nextByte);

        string res = sBuilder.ToString();

Then store it in the database by passing the string. You should try and get some extra work out of your client by offering to update their database structure though ;)

jcharlesworthuk
  • 1,079
  • 8
  • 16