2016-12-27 67 views
-1
Byte[] bytes; 
string qry = "SELECT seller.sid,seller.fname, seller.img, seller.lname, seller.contact, seller.emailid, login.status FROM seller INNER JOIN login ON seller.lid = login.lid where login.status =1 and [email protected]"; 
SqlCommand cmd = new SqlCommand(qry, con); 
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; 
if (bytes == null) { 
    bytes = File.ReadAllBytes("noimg.jpg"); 
} 

如何在img列顯示noimg.jpg爲空時。我試圖這樣做,但它不工作。我可以做什麼? 在此先感謝使用未分配的局部變量'字節'

回答

1

C#編譯器希望你使用它之前,你要提供價值變量,

Byte[] bytes = null; 
+0

downvote之前我完成了:( – Sajeetharan

0

初始化字節數組,因爲你給它分配值之前使用,以避免錯誤。

Byte[] bytes = null; 

但是,這將始終將空值傳遞給您的查詢。首先填充字節數組,然後將其作爲參數傳遞給您的查詢。

Byte[] bytes = File.ReadAllBytes("noimg.jpg");; 
string qry = "SELECT seller.sid,seller.fname, seller.img, seller.lname, seller.contact, seller.emailid, login.status FROM seller INNER JOIN login ON seller.lid = login.lid where login.status =1 and [email protected]"; 
SqlCommand cmd = new SqlCommand(qry, con); 
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; 
1

我建議宣佈byte[] bytes接近北京時間用作可能:

// do not declare any local variable prematurely: do we want bytes here? No 

// Make query readable: it helps to see that filtering (where) is very strange 
string qry = 
    @"SELECT seller.sid, 
      seller.fname, 
      seller.img, 
      seller.lname, 
      seller.contact, 
      seller.emailid, 
      login.status 
     FROM seller INNER JOIN 
      login ON seller.lid = login.lid 
    WHERE login.status = 1 AND 
      seller.img = @Data"; // filtering on IMAGE?!! Very dubiuos solution 

// wrap IDisposable (SqlCommand) into using 
using(SqlCommand cmd = new SqlCommand(qry, con)) { 
    // And here we want byte[] bytes as a storage for the image: 
    byte[] bytes = File.ReadAllBytes("noimg.jpg"); 

    cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; 

    ... 
}