RE: EF or LINQtoSQL and VarBinary Max?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi SnapDive,

Gregory is correct that in LINQ to SQL, varbinary(max) is bound to
System.Data.Linq.Binary or byte[], while in LINQ to Entities, it is
generally bound to byte[]. To do CRUD operations on such tables with
varbinary(max) via LINQ to SQL and LINQ to Entities, you can see the
following codes for references:

Here is a helper method to read images into byte[]:
===================================================================
private static byte[] ReadImage(string path)
{
try
{
// Open the image file
using (FileStream fs = new FileStream(path, FileMode.Open,
FileAccess.Read))
{
// Create an array of bytes
byte[] bPicture = new byte[fs.Length];

// Read the image file
fs.Read(bPicture, 0, Convert.ToInt32(fs.Length));

return bPicture;
}
}
catch (Exception ex)
{
Console.WriteLine(
"The application throws the error: {0}", ex.Message);
return null;
}
}
===================================================================

LINQ to SQL: (for Binary, if mapping to byte[], the codes is similar with
the LINQ to Entities part)
===================================================================
Insert:
MyDataContext db = new MyDataContext();
MyImageTable myImage = new MyImageTable()
{
ImageID = 2,
MyImage = ReadImage("C#.jpg")
};
db.MyImageTables.InsertOnSubmit(myImage);
db.SubmitChanges();

Read:
var myImage = db.MyImageTables.Where(i => i.ImageID == 2).Single();
MemoryStream stream = new MemoryStream();
stream.Write(myImage.MyImage.ToArray(), 0, myImage.MyImage.Length);
Image image = Image.FromStream(stream);

Search:
byte[] imageBytes = ReadImage("C#.jpg");
var query = from i in db.MyImageTables
where i.MyImage == imageBytes
select i;
MessageBox.Show(query.First().MyImage.Length.ToString());

Delete:
db.MyImageTables.DeleteOnSubmit(myImage);
db.SubmitChanges();
===================================================================

LINQ to Entities:
===================================================================
Insert:
MyEntities entities = new MyEntities();
MyImageTable myImage = new MyImageTable();
myImage.ImageID = 2;
myImage.MyImage = ReadImage("C#.jpg");
entities.AddToMyImageTable(image);
entities.SaveChanges();

Read:
MemoryStream stream = new MemoryStream();
MyImageTable myImage = entities.MyImageTable.First();
stream.Write(myImage.MyImage, 0, myImage.MyImage.Length);
Image image = Image.FromStream(stream);

Search:
byte[] imageBytes = ReadImage("C#.jpg");
var query = from i in entities.MyImageTable
where i.MyImage == imageBytes
select i;

Delete:
entities.DeleteObject(myImage);
entities.SaveChanges();
===================================================================

If you encounter any problems, please feel free to let me know.

Have a nice day!

Best Regards,
Lingzhi Sun
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

.



Relevant Pages

  • =?ISO-8859-1?Q?Extending_Entity_Framework_=A0and_LINQ_to_SQL_Support?= =?ISO-8859-1?
    ... Entity Framework support, LINQ to SQL support, and contain an ORM ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Extending SQL LINQ Table Class
    ... There are a series of LINQ to SQL articles on ScottGu's blog. ... Microsoft MSDN Online Support Lead ... Extending SQL LINQ Table Class ...
    (microsoft.public.dotnet.framework)
  • RE: Association missing in dbml generated classes
    ... How to implement a many-to-many relationship using Linq to Sql? ... Microsoft Online Community Support ... You can send feedback directly to my manager at: ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: linq troubles
    ... > and we're isolating the problem in the ms sql server, ... > set up linq to free the used resources. ... Linq-to-SQL like it will support and enhance the ADO.NET Entity Framework. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Are Linq-SQL methods commutative
    ... No - in LINQ each step effectively works off the result of the ... linq query which has to meet a SQL query they have in mind. ... freedom a SQL statement gives them: the SQL statement is the one ...
    (microsoft.public.dotnet.languages.csharp)