2012-06-05 69 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