Loading and Inserting Blob Data Types using LINQ to SQL in ASP.NET using C#

I would like to know how i can use LINQ to store blob data types. I basically want to store binary data files like documents in an sql server database. i have done this the traditional was using SQL but i'm not sure whether LINQ supports it.

Any pointers please?
TOPICTAsked:
Who is Participating?
 
tovvenkiCommented:
I think you can do that using byte[].
I am sorry i do not have VS right now to test it.

see if this helps you
http://www.velocityreviews.com/forums/t637024-linq-and-blobs.html

Thanks and regards,
Venki
0
 
AshokCommented:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
using System.Configuration;

.....

    SqlConnection conn;
    string sConnStr;
    sConnStr = "Set your Connection String here...........";

      try
      {
        conn = new SqlConnection(sConnStr);
        string sSQL = "UPDATE tbSomeTable SET Image = (@BlobData) " +
                      "where GroupID = " + txtGrpID.Text.Trim() +
                      " and ItemID =  " + txtItemID.Text.Trim();
        //SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", conn);
        SqlCommand cmd = new SqlCommand(sSQL, conn);
        String strBLOBFilePath;
        openFileDialog1.Title = "Select an Image";
        openFileDialog1.Filter = "Icons|*.ico|Bitmap|*.bmp|JPEG|*.jpg|GIF Images|*.gif|PNG Images|*.png";
        openFileDialog1.FileName = "";
        if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
        {
          strBLOBFilePath = @"C:\MyImage.ICO";//Modify this path as needed.
          strBLOBFilePath = @openFileDialog1.FileName;
        }
        else
        {
          return;
        }
        //Read jpg into file stream, and from there into Byte array.
        FileStream fsBLOBFile = new FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read);
        Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
        fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
        fsBLOBFile.Close();

        //Create parameter for insert command and add to SqlCommand object.
        SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
              0, 0, null, DataRowVersion.Current, bytBLOBData);
        cmd.Parameters.Add(prm);

        //Open connection, execute query, and close connection.
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

HTH
Ashok
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.