mitsu
mitsu

Reputation: 430

How can I get image from SQL via C#?

I have a form with information about of every Person in a society.

In this form there is an image which must be generated from SQL server. I don't know how to generate this image Fromm sql to c# in image control using a stored procedure which display image according to a CODE. I'm working in an application in ASP(c#)

this is my procedure in sql :

create proc uploadImage 
@ppr int 
as

 if exists (select idimg from Images where idimg = ( select codeimg from Agent where PPR=@ppr))
 begin 
    select image from Images where idimg = ( select codeimg from Agent where PPR=@ppr) 
 end 

i have already my code but it's not completed :

try
            {
                c.cmd = c.cn.CreateCommand();
                c.cmd.CommandText = "uploadImage";
                c.cmd.CommandType = CommandType.StoredProcedure;
                if (c.cn.State == ConnectionState.Closed)
                {
                    c.cn.Open();
                }
                c.cmd.Parameters.Add("@ppr", SqlDbType.Int);
                c.cmd.Parameters["@ppr"].Value = Session["Code"];
                SqlDataReader dr = c.cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                     **// here where i will treat my image ..i think**   
                }
                dr.Close();
            }

Upvotes: 0

Views: 144

Answers (1)

bdn02
bdn02

Reputation: 1500

I think that he correct way is:

  1. Create a stored procedure that read the image data
  2. Create c# class HttpHandler that call db stored and write to the response the binary content (with the correct content-type) (dotnet framework "view" sqlserver image columns as byte[])
  3. Put in you form a component of type image that have as url the httphandler with a parameter that specify the data to read

Upvotes: 2

Related Questions