2013-05-28 113 views
0

您好我想從sql server數據庫檢索圖像使用此代碼和異常生成此錯誤(參數無效)PLZ幫助我,我在表中使用圖像類型沒有一個blob從sql server 2008數據庫檢索圖像使用c#

SqlCommand select = new SqlCommand(vsql, vCon); 
     try 
     { 
      SqlDataAdapter dp = new SqlDataAdapter(select); 
      DataSet ds = new DataSet("etudiant"); 
      byte[] MisDatos = new byte[0]; 
      dp.Fill(ds,"etudiant"); 
      DataRow myRow = ds.Tables["etudiant"].Rows[0]; 
      MisDatos = (byte[])myRow["image"]; 
      MemoryStream ms = new MemoryStream(MisDatos); 
      System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms); 
      ms.Position = 0; 
      pictureBox1.Image = Image.FromStream(ms); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
+1

哪個參數? –

+0

跳過dataadapter和數據集並直接用datareader檢索它可能會更好。 –

+0

[鏈接](http://social.msdn.microsoft.com/Forums/getfile/190061)。 – user2430314

回答

0

下面是使用DataReader的對網頁代碼中的BLOB:

using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
    { 
     conn.Open(); 
     using (SqlCommand cmd = new SqlCommand("SELECT photo,photomimetype,photofilename FROM MyTable WHERE [email protected]", conn)) 
     { 
      cmd.Parameters.Add("@photoid", SqlDbType.Int).Value = photoId; 
      using(SqlDataReader dr = cmd.ExecuteReader()) 
      { 
       if (dr.Read()) 
       { 
        Response.ContentType = dr["photomimetype"].ToString(); 
        Response.AddHeader("content-disposition", "inline; filename=\"" + dr["photofilename"] + "\""); 
        Response.BinaryWrite((byte[])dr["photo"]); 
       } 
       else 
       { 
        Response.StatusCode = 404; 
       } 
       dr.Close(); 
      } 
     } 
    } 
相關問題