RE: EF or LINQtoSQL and VarBinary Max?
- From: v-micsun@xxxxxxxxxxxxxxxxxxxx (Lingzhi Sun [MSFT])
- Date: Wed, 26 Aug 2009 06:28:14 GMT
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.
.
- Follow-Ups:
- RE: EF or LINQtoSQL and VarBinary Max?
- From: Lingzhi Sun [MSFT]
- RE: EF or LINQtoSQL and VarBinary Max?
- References:
- EF or LINQtoSQL and VarBinary Max?
- From: SnapDive
- EF or LINQtoSQL and VarBinary Max?
- Prev by Date: System.Windows.RoutedEventArgs error
- Next by Date: Changing connection string of a strong typed dataset
- Previous by thread: Re: EF or LINQtoSQL and VarBinary Max?
- Next by thread: RE: EF or LINQtoSQL and VarBinary Max?
- Index(es):
Relevant Pages
|