2011-11-29 31 views
1

我試着檢索查詢作爲一個字節數組。儘管我堅持@代碼。Retireve查詢爲字節數組

byte[] temp = QueryFile("SELECT modelFile FROM items WHERE modelName='F_Pants1'"); 
    public byte[] QueryFile(string queue) 
      { 
       MySqlCommand command = MySqlCon.CreateCommand(); 
       MySqlDataReader Reader; 
       command.CommandText = queue; 
       MySqlCon.Open(); 
       Reader = command.ExecuteReader(); 
       byte[] thisrow = new byte[1000]; 
       while (Reader.Read()) 
       { 
        for (int i = 0; i < Reader.FieldCount; i++) 
         thisrow[0] = Convert.ToByte(Reader.GetValue(i).ToString()); 


       } 
       thisrow = thisrow.Remove(thisrow.Length - 1, 1); 
       MySqlCon.Close(); 

       return thisrow; 

      } 

如果有人有答案,我將不勝感激。

+0

函數接受完整的sql查詢作爲字符串將導致sql注入安全問題。 –

+0

SQL注入,如果功能將得到未覈對的字符串。 –

回答

4

你正在做的有點太複雜了。試試這個:

 try { 
     Reader = command.ExecuteReader(); 
     if (Reader.Read()) 
      { 
       return Reader.GetValue(0) as Byte[]; 
      } 
     } finally { 
      MySqlCon.Close(); 
     } 
+2

這也太複雜了 - 他可以只使用.ExecuteScalar() –

+0

@JoelCoehoorn:好一點。 –

+0

後來生病了,但是當我查詢時它返回null –

相關問題