2

The type of my database column is varbinary(max), (sql server 2008).

Linq to Sql Classes (vs 2010, fx4) has generated this code for me:

[global::System.Data.Linq.Mapping.ColumnAttribute
(Storage="_Raw", DbType="VarBinary(MAX)", UpdateCheck=UpdateCheck.Never)]
public System.Data.Linq.Binary Raw
{
  get
  {
    return this._Raw;
  }
  ...

I want to insert a 1.5 Mb bitmap file like so:

var fi = new FileInfo(@"c:\whatever.bmp");
var fs = fi.OpenRead();
var newImage = new Image();

//var buffer = new byte[fs.Length];
//fs.Read(buffer, 0, (int)fs.Length);
//newImage.Raw = buffer;

var buffer = new byte[4000];
fs.Read(buffer, 0, 4000);
newImage.Raw = buffer;

using (var dc = new MyDataContext())
{
  dc.Images.InsertOnSubmit(newImage);
  dc.SubmitChanges();
}
fs.Dispose();

The lines I've commented out are what I want the code to be, but they throw an exception at the SubmitChanges call. The lines underneath do work ok. But if I increase the buffer size to 4001, I get the same exception. I'm going to need to have an fs.Length of more like 1,500,000.

The exception message is "String or binary data would be truncated." Can anyone explain this error and show me how to get it working?

wozza
  • 255
  • 2
  • 7
  • The exception you mention, is that a the sql error you getting or something else? – leppie Jul 28 '11 at 06:32
  • It's the message from the exception thrown on the call to SubmitChanges. I suspect it's a message from sql server and just wrapped into a .net exception. I've edited to explain it better. – wozza Jul 28 '11 at 07:19
  • I suspect there is some kind of issue in SQL then. Try inserting the data without Linq2SQL to confirm. – leppie Jul 28 '11 at 07:21
  • I used ADO.Net and I got the same SqlException. – wozza Aug 01 '11 at 02:00

1 Answers1

0

The varbinary(max) data type supports upto 2^31-1 bytes which means that it is probably not this data type that is causing the problem.

Could you please provide the type of the exception? The error message sounds very much like it is coming from the database.

I would double check the definition for the column to ensure that it really is a varbinary(max).

Try using SQL Profiler to see what statement is actually being sent to the database. Unless Linq To Sql is somehow generating more than 2^31-1 bytes out of the 4001 that you are sending, it is unlikely to be Linq To Sql that is causing the problem.

Scott Munro
  • 13,369
  • 3
  • 74
  • 80
  • There are many questions on Stackoverflow that were solved with a quick check of the documentation. You have obviously done your homework though. Sorry about the unintended insult. I would still be interested to know the type of the exception. It sounds like it might be a SqlException which is one that wraps an SQL error (which is what you are expecting based on your other comment). In this case, I would try to reproduce the problem after removing Linq To Sql from the scope of the problem. This is also what @leppie recommended. Check Exception.InnerException to find the core exception. – Scott Munro Aug 01 '11 at 02:15