从数据库下载照片c#

时间:2012-06-05 18:43:39

标签: c# asp.net sql

我尝试从数据库下载照片。在数据库中,字段类型是Image。 ID是UniqueIdentifier类型。 我的代码

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

    // Create SQL Command 
    SqlCommand cmd = new SqlCommand("Select ID,photo from profile where ID=@ID", 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。 “无法将参数值从String转换为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>"));

2 个答案:

答案 0 :(得分:4)

试试这个,看它是否有效:

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

这里的关键是将字符串转换为GUID。

修改

我注意到您所做的编辑包括您的呼叫处理程序。我在您的调用处理程序中看到的两个明显的问题是您创建了一个id变量来保存转换后的GUID,但您并没有真正使用它。此外,您没有使用和号正确分隔边框查询参数:&amp;

改变这个:

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作为您的ID:5a6b4047-e2dc-40d8-9c58-8609278154f4border=1,这不是有效的GUID。

就像提示一样,我会这样做,这可能会更容易发现你的Type-O:

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

答案 1 :(得分:0)

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

ImageID.Value = ImageGUID;