locked
how can get the image from sqlserver Db into my report. RRS feed

  • Question

  • Hi,

    I want to get the Jpeg image in my report.

    Where i'll be having the folder path of the image in the table.

    help me out in solving this.

    Thanx.

    • Changed type Jackie-Sun Tuesday, March 22, 2011 9:16 AM question
    Tuesday, March 15, 2011 6:43 AM

Answers

  • Hello Arsath,

     

    Welcome to the MSDN Forum and thanks for posting here.

    We can write a binary large object (BLOB) to a database as either binary or character data, depending on the type of field at the data source. BLOB is a generic term that refers to the text, ntext, and image data types, which typically contain documents and pictures.

    To write a BLOB value to the database, issue the appropriate INSERT or UPDATE statement and pass the BLOB value as an input parameter. If the BLOB is stored as text, such as a SQL Server text field, we can pass the BLOB as a string parameter. If the BLOB is stored in binary format, such as a SQL Server image field, we can pass an array of type byte as a binary parameter.

    The following code example adds employee information to the Employees table in the Northwind database. A photo of the employee is read from a file and added to the Photo field in the table, which is an image field.

    Codes:

            public static void AddEmployee(string lastName,string firstName,string title,

                DateTime hireDate,int reportsTo,string photoFilePath,string connectionString)

            {

                byte[] photo = GetPhoto(photoFilePath);

     

                using (SqlConnection connection = new SqlConnection(

                  connectionString))

                {

                    SqlCommand command = new SqlCommand(

                      "INSERT INTO Employees (LastName, FirstName, " +

                      "Title, HireDate, ReportsTo, Photo) " +

                      "Values(@LastName, @FirstName, @Title, " +

                      "@HireDate, @ReportsTo, @Photo)", connection);

     

                    command.Parameters.Add("@LastName",

                       SqlDbType.NVarChar, 20).Value = lastName;

                    command.Parameters.Add("@FirstName",

                        SqlDbType.NVarChar, 10).Value = firstName;

                    command.Parameters.Add("@Title",

                        SqlDbType.NVarChar, 30).Value = title;

                    command.Parameters.Add("@HireDate",

                         SqlDbType.DateTime).Value = hireDate;

                    command.Parameters.Add("@ReportsTo",

                        SqlDbType.Int).Value = reportsTo;

     

                    command.Parameters.Add("@Photo",

                        SqlDbType.Image, photo.Length).Value = photo;

     

                    connection.Open();

                    command.ExecuteNonQuery();

                }

            }

     

            public static byte[] GetPhoto(string filePath)

            {

                FileStream stream = new FileStream(

                    filePath, FileMode.Open, FileAccess.Read);

                BinaryReader reader = new BinaryReader(stream);

     

                byte[] photo = reader.ReadBytes((int)stream.Length);

     

                reader.Close();

                stream.Close();

     

                return photo;

            }

     

    Related Threads:

    http://www.redmondpie.com/inserting-in-and-retrieving-image-from-sql-server-database-using-c/

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/c85f02c0-92c0-4e55-aaec-36bed876390e

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/010d9387-d6e1-4884-8cf4-051540b36104

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Tuesday, March 22, 2011 9:16 AM
    Thursday, March 17, 2011 6:16 AM