2016-05-17 67 views
0

當我把鼠標放在DownloadFile上時,它說(並不是所有的路徑都返回一個值)。我需要做些什麼來解決這個問題?C#不是所有的路徑返回值

public Stream DownloadFile() 
{ 
    using (SqlConnection con = new SqlConnection("GAPDB")) 
    { 
     using (SqlCommand cmd = new SqlCommand("SELECT FileUpload FROM [FirmWareVersion]", con)) 
     { 
      con.Open(); 

      SqlDataReader reader = cmd.ExecuteReader(); 
      if (reader.Read()) 
      { 
       byte[] data = (byte[])reader["FileUpload"]; 
       System.IO.MemoryStream ms = new System.IO.MemoryStream(data); 
      } 
     } 
    } 
} 
+0

添加其他條件返回一些東西或'null' –

+2

編譯器錯誤很明顯:甚至不是所有的路徑都返回一個值,而不是一個_single_路徑返回一個值!所以,在你的if塊中添加一個'return ms;'並且可能是一個'return null;'在你的方法結尾。 –

+0

您定義了一個返回Stream的函數。但在函數中,你不會返回任何東西 –

回答

3

您需要返回Stream爲您的方法定義了Stream作爲返回類型:

public Stream DownloadFile() 
{ 
    using (SqlConnection con = new SqlConnection("GAPDB")) 
    { 
     using (SqlCommand cmd = new SqlCommand("SELECT FileUpload FROM [FirmWareVersion]", con)) 
     { 
      con.Open(); 

      SqlDataReader reader = cmd.ExecuteReader(); 
      if (reader.Read()) 
      { 
       byte[] data = (byte[])reader["FileUpload"]; 
       System.IO.MemoryStream ms = new System.IO.MemoryStream(data); 
       return ms; 
      } 
     } 
    } 

    return null; 
} 

另外,您可以標記您的方法void,如果你不需要從它返回。

+0

Thanx工作! –

相關問題