2012-06-05 68 views
1

我嘗試從數據庫下載照片。在數據庫中,字段類型是Image。 該ID是類型UniqueIdentifier。 我的代碼從數據庫c下載照片#

public void ProcessRequest (HttpContext context) { 
    ConnectTodatabase conection = new ConnectTodatabase(); 
    conection.makeConnection(); 

    // Create SQL Command 
    SqlCommand cmd = new SqlCommand("Select ID,photo from profile where [email protected]", conection.Connection); 
    SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.UniqueIdentifier); 
    ImageID.Value = context.Request.QueryString["ID"]; 
    cmd.Parameters.Add(ImageID); 

    SqlDataReader dReader = cmd.ExecuteReader(); 
    dReader.Read(); 
    context.Response.BinaryWrite((byte[])dReader["photo"]); 
    dReader.Close(); 


    context.Response.ContentType = "text/plain"; 
    context.Response.Write("test 123456"); 
} 

異常是InvalidCastException。 「無法將參數值從字符串轉換爲Guid。」 如何傳遞ImageID到正確的類型? 謝謝!

調用句柄

foreach (DataRow theRow in thisDataSet.Tables["Profile"].Rows) 
     { 
      resultCounter++; 
      double x, y; 
      x = Convert.ToDouble(theRow["lat"]); 
      y = Convert.ToDouble(theRow["lng"]); 
      string id = Convert.ToString(theRow["ID"]); 
      GLatLng latlng = new GLatLng(x, y);//sintetagmenes shmeiou 
      //dimiourgia ton 2 ipomenou gia kathe shmeio 
      GInfoWindowTabs iwTabs = new GInfoWindowTabs(); 
      iwTabs.point = latlng; 

      System.Collections.Generic.List<GInfoWindowTab> tabs = new System.Collections.Generic.List<GInfoWindowTab>(); 

      tabs.Add(new GInfoWindowTab("Profile Info:", "<table> <tr> <td><b>Name: </b> </td><td>" + theRow["fname"] + "</td></tr><tr><td><b>Lastname: </b></td><td>" + theRow["lname"] + "</td></tr><tr><td><b>Affiliation: </b></td><td>" + theRow["affiliation"] + "</td></tr><tr><td><b>Address: </b></td><td>" + theRow["address"] + "</td></tr><tr><td><b>Country: </b></td><td>" + theRow["country"] + "</td></tr><tr><td><b>Email: </b></td><td>" + theRow["email"] + "</td></tr><tr><td><b>Role: </b></td><td>" + theRow["role"])); 


      tabs.Add(new GInfoWindowTab("Profile Photo:", "<img src=Handler.ashx?ID=" + theRow["ID"] + "border=1>")); 

回答

4

試試這個,看看它是否工作:

SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.UniqueIdentifier); 
ImageID.Value = System.Data.SqlTypes.SqlGuid.Parse(context.Request.QueryString["ID"]); 

這裏的關鍵是在字符串轉換爲一個GUID。

編輯

我注意到,你所做的修改包括呼叫處理程序。我在調用處理程序中看到的兩個突出問題是,您創建了一個id變量來保存轉換後的GUID,但是您沒有使用它。 &

更改此:

tabs.Add(new GInfoWindowTab("Profile Photo:", "<img src=Handler.ashx?ID=" + theRow["ID"] + "border=1>")); 

這樣:還有,你沒有正確使用符號劃定你的邊界查詢參數

tabs.Add(new GInfoWindowTab("Profile Photo:", "<img src=Handler.ashx?ID=" + id + "&border=1>")); 

因此,在本質上,你是發送這個ID爲:5a6b4047-e2dc-40d8-9c58-8609278154f4border=1這不是一個有效的GUID。

正如一個提示,我會做這樣這將可能使得它更容易發現你的O型:

tabs.Add(new GInfoWindowTab("Profile Photo:", string.Format("<img src=Handler.ashx?ID={0}&border=1>", id))); 
+0

我有這樣的錯誤: 的Guid應包含32位用4破折號(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。 – Jimmysnn

+0

從ASP.NET配置中創建新用戶時,Id通過visual studio自動創建 – Jimmysnn

+1

顯示示例ID。需要確保它實際上是一個GUID。 – Jeremy

0
Guid ImageGUID = new Guid(context.Request.QueryString["ID"]); 

ImageID.Value = ImageGUID; 
+0

我有同樣的錯誤: Guid應該包含32個數字,並且有4個破折號(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。 從ASP.NET配置中創建新用戶時,Id通過visual studio創建了自動化 – Jimmysnn

+0

GUID對我來說很好。你確定它沒有圍繞它或引號嗎? – TimWagaman

+0

是的,我檢查它。爲我的問題添加更多代碼 – Jimmysnn