Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
in a tabel i have a field which its name is images.this tabel have many records.
i dont know how i can read all images from this tabel.
this code just take me 1 image;the first one:
C#
string strselect="SELECT bannerImg FROM tb_banner" ;

SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbtestConnectionString"].ConnectionString);
        SqlCommand sqlcom1 = new SqlCommand(strselect, sqlcon);
        sqlcon.Open();
        
        object img=sqlcom1.ExecuteScalar();
        
        if (img=!null)
        {
            Response.BinaryWrite((byte[])img);
            sqlcon.Close();
        }
Posted
Updated 16-Jan-12 21:12pm
v2
Comments
Anuja Pawar Indore 17-Jan-12 3:12am    
Added pre tag

It's not difficult - you can see how I do it here: A generic Image-From-DB class for ASP.NET[^]
 
Share this answer
 
You are using ExecuteScalar while I guess it should ExecuteReader.
 
Share this answer
 
Comments
maliheSadat 17-Jan-12 5:20am    
i use ExecuteScalar because the output of select is one parameter(bannerImg)
dan!sh 17-Jan-12 6:14am    
ExecuteScalar is not about number of columns. It is number of values that are returned. You need multiple rows so ExecuteReader is the way to go.
VB
Function SaveImage _
   (ByVal FileName As String) As Integer

   ' Create a FileInfo instance
   ' to retrieve the files information
   Dim fi As New FileInfo(FileName)

   ' Open the Image file for Read
   Dim imgStream As Stream = _
      fi.OpenRead

   ' Create a Byte array the size
   ' of the image file for the Read
   ' methods buffer() byte array
   Dim imgData(imgStream.Length) As Byte
   imgStream.Read(imgData, 0, fi.Length)

   Dim cn As New SqlConnection _
      (ConfigurationSettings.AppSettings("cn"))
   Dim cmd As New SqlCommand("Images_ins", cn)
   cmd.CommandType = CommandType.StoredProcedure

   With cmd.Parameters
      .Add("@FileName", VarChar, 100).Value = _
         fi.Name
      .Add("@FileType", VarChar, 10).Value = +
         fi.Extension
      .Add("@Image", Image).Value = imgData
      .Add("@FileSize", Int, 4).Value = fi.Length
   End With

   cn.Open()
   cmd.ExecuteNonQuery()
   cn.Close()
   cn.Dispose()
End Function
 
Share this answer
 
v2
Comments
demouser743 17-Jan-12 4:04am    
Added pre tag
maliheSadat 17-Jan-12 5:28am    
i asked this question in c# part! you answered in vb!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900