现在的需求  将图片以二进制的方式存入或者读取 数据库中 如果在一个页面里 我可以写出来 也实现出来过 但我想将这个方法封装起来 方便调用 还请大侠帮帮忙
一个是存入数据库的 一个是从数据库中读取的 

解决方案 »

  1.   

    不能直接用byte[]? 不需要定义成类吧?
      

  2.   

    读取的   
    string sql = string.Empty;
                SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=findFriend");
                con.Open();
                sql="......";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataReader sdr = cmd.ExecuteReader();
                byte[] buffer = null;
                while (sdr.Read())
                {
                  
                    buffer = (byte[])sdr["image"];
                    Response.ContentType = "application/octet-stream";
                    Response.BinaryWrite(buffer);            }
             
                con.Close();
                sdr.Close();
                Response.End();插入的  //二进制数组
            byte[] buffer = new byte[size];
            Stream picstream = FileUpload1.PostedFile.InputStream;
            picstream.Read(buffer, 0, size);
            string sql = string.Empty;
            SqlCommand cmd;
            int iRet = 0;
            UserInfo ui = (UserInfo)Session["user"];
            SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123;database=findFriend");
            conn.Open();
            sql = string.Format("select COUNT(*) from photo where UId={0} and head=1", ui.id);
            cmd = new SqlCommand(sql, conn);
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            if (i > 0)
            {
                sql = string.Format("update photo set size='{0}',type='{1}',name='{2}',image=@image  where UId='{3}'",
                    size, type, name,ui.id);            cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add("@image", SqlDbType.Image);
                cmd.Parameters["@image"].Value = buffer;
                iRet = cmd.ExecuteNonQuery();
                Panel1.Visible = true;
                Panel2.Visible = false;
            }