【问题标题】:download a photo from database c#从数据库c#下载照片
【发布时间】:2012-06-05 18:43:39
【问题描述】:

我尝试从数据库中下载一张照片。在数据库中,字段类型是图像。 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。 “无法将参数值从字符串转换为 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>"));

【问题讨论】:

    标签: c# asp.net sql


    【解决方案1】:

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

    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。

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

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

    【讨论】:

    • 我有这个错误:Guid 应该包含 32 位数字和 4 个破折号 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。
    • 从 ASP.NET 配置创建新用户时从 Visual Studio 自动创建的 Id
    • 显示示例 ID。需要确保它实际上是一个 GUID。
    • 5a6b4047-e2dc-40d8-9c58-8609278154f4
    • 是的,这是有效的。现在,您是否已验证 context.Request.QueryString["ID"] 包含有效 ID?添加断点并对其进行分析以确保。
    【解决方案2】:
    Guid ImageGUID = new Guid(context.Request.QueryString["ID"]);
    
    ImageID.Value = ImageGUID;
    

    【讨论】:

    • 我有同样的错误:Guid 应该包含 32 位数字和 4 个破折号 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。从 ASP.NET 配置创建新用户时,从 Visual Studio 自动创建的 Id
    • 那个 GUID 对我来说很好。您确定它周围没有 {} 或引号吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多