0

我正在做一个 3 层应用程序来从 sql server 检索图像,我将图像存储到 sql 中的二进制数据中,问题是我无法从 sql server 检索我的图像。

这是我在 DataAccessLayer 中的代码

  public List<Volunteer> VolunteerTRetrieve()
    {
        List<Volunteer> vList = new List<Volunteer>();
        byte[] volunteerProfilePicture;
        string volunteerName, volunteerNRIC, volunteerAddress, volunteerEmail, volunteerContact;
        string queryStr = "SELECT * FROM TVolunteer Order By VolunteerName";
        SqlConnection conn = new SqlConnection(DBconnStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while ((dr.Read()))
        {
            volunteerName = dr["VolunteerName"].ToString();
            volunteerNRIC = dr["VolunteerNRIC"].ToString();
            volunteerAddress = dr["VolunteerAddress"].ToString();
            volunteerEmail = dr["VolunteerEmail"].ToString();
            volunteerContact = dr["VolunteerContact"].ToString();
            volunteerProfilePicture = (byte[])dr["VolunteerProfilePicture"];

            vList.Add(new Volunteer(volunteerName, volunteerNRIC, volunteerAddress, volunteerEmail, volunteerContact, volunteerProfilePicture));
        }
        conn.Close();
        dr.Dispose();
        return vList;
    }

在业务逻辑层

   public List<Volunteer> RetrieveAllBVolunteer()
    {
        Volunteer v = new Volunteer();
        List<Volunteer> vList = new List<Volunteer>();
        vList = v.VolunteerBRetrieve();
        return vList;
    }

在 PresentationLayer

   List<Volunteer> allVolunteer = new List<Volunteer>();
   allVolunteer = vBLL.RetrieveAllTVolunteer();
   dl_tvolunteer.DataSource = allVolunteer;
   dl_tvolunteer.DataBind();

我还有一个图像处理程序类

public class ShowImage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["DBconnStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        string queryStr = "SELECT VolunteerProfilePicture FROM TVolunteer WHERE VolunteerNRIC = @NRIC";
        SqlCommand cmd = new SqlCommand(queryStr, conn);
        cmd.Parameters.Add("@NRIC", SqlDbType.VarChar).Value =
            context.Request.QueryString["VolunteerNRIC"];
        cmd.Prepare();
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((byte[])dr["VolunteerProfilePicture"]);
    }

请帮忙,谢谢!

4

1 回答 1

0

如果您要返回一张图片,您可以执行以下操作

1. place a PictureBox control on the asp.net webpage
2. define the sql connections //not sure why you are using cmd.prepare

byte[] sqlImage = (byte[])cmd.ExecuteScalar();
MemoryStream memStream = new MemoryStream();
memStream.Write(sqlImage, 0, sqlImage.Length);
Bitmap bit = new Bitmap(memStream);

或者如果你想以不同的方式做

  try

  {

    con = new SqlConnection(constr);
    cmd = new SqlCommand("select photopath,Photo from employees where employeeid=14", con);
    con.Open();
    dr = cmd.ExecuteReader();
            while(dr.Read())
            {
                if (!dr.IsDBNull(1))
                {
                    byte[] photo = (byte[])dr[1];
                    MemoryStream ms = new MemoryStream(photo);
                    pictureBox1.Image = Image.FromStream(ms);
                }


            }
        }
        catch (Exception ex)
        {
            dr.Close();
            cmd.Dispose();
            con.Close();

            Response.Write(ex.Message);
        }
于 2012-12-23T15:14:18.680 回答