1

データベースから写真をダウンロードしようとしています。データベースでは、フィールド タイプは画像です。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>"));
4

2 に答える 2

4

これを試して、機能するかどうかを確認してください。

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

ここで重要なのは、文字列をGUIDに変換することです。

編集

行った編集にコールハンドラが含まれていることに気付きました。コールハンドラーで私が目にする2つの明白な問題は、変換されたGUIDを保持するためのid変数を作成したが、それを実際に使用していないことです。また、アンパサンドを使用して境界クエリパラメータを適切に区切っていません。&

これを変える:

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)));
于 2012-06-05T18:47:38.220 に答える
0
Guid ImageGUID = new Guid(context.Request.QueryString["ID"]);

ImageID.Value = ImageGUID;
于 2012-06-05T18:50:51.170 に答える