如何在asp.net中显示从数据库到图像控件的图像

时间:2017-07-22 07:27:26

标签: asp.net

我想从数据库中检索图像,但这段代码没有显示任何内容。 数据库中的图像数据类型是image.I不知道这段代码有什么问题

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(Session["pro_ID"]);

    int id = Convert.ToInt32(Session["pro_id"]);
    SqlConnection conn = new SqlConnection("Data source=DESKTOP-QPTTS3M;initial catalog=shopolic;integrated security=True;");

    conn.Open();
    SqlCommand cmd = new SqlCommand("spgetimage", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter param = new SqlParameter() { ParameterName = "@id", Value = Session["pro_id"] };
    cmd.Parameters.Add(param);
    //cmd.ExecuteScalar();
    SqlCommand cmd2 = new SqlCommand("select  *from product where product_ID='" + Session["pro_id"] + "'", conn);
    //  cmd2.ExecuteNonQuery();
    SqlDataReader dr = cmd2.ExecuteReader();
    if (dr.Read())
    {
        name.Text = dr["name"].ToString();
        byte[] img = (byte[])(dr["image"]);
        if (img == null)
        {
            Image1.ImageUrl = "~/Images/bg.png";

        }
        else
        {
            string base64String = Convert.ToBase64String(img);
            Image1.ImageUrl = String.Format("data:image/jpg;base64,{0}", base64String);
            //     MemoryStream ms = new MemoryStream(img);
        }
    }
    //  byte[] bytes = (byte[])cmd.ExecuteScalar();
    // string strbase64 = Convert.ToBase64String(bytes);
    //Image1.ImageUrl = "data:Image/png;base64,"+strbase64;
    //SqlDataReader dr = cmd.ExecuteReader();
    //DataTable DT = new DataTable();
    conn.Close();
}

0 个答案:

没有答案