业务逻辑层
public SqlDataReader SelectNewsByGuid(Guid id) { 
            ConfigManager cm = new ConfigManager();
            SqlConnection conn = new SqlConnection(cm.DalConnectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from NewsItem where Guid=@guid", conn);
            cmd.Parameters.Add("@guid", SqlDbType.UniqueIdentifier).Value = id;
            SqlDataReader dr = cmd.ExecuteReader();
            return dr;
        }
web层
 context.Response.ContentType = "text/plain";
            NewsManager manager = new NewsManager();
            Guid id = new Guid(HttpContext.Current.Request.Params["guid"].ToString());
            //业务逻辑层根据id获取新闻信息
            SqlDataReader dr = manager.SelectNewsByGuid(id);
            if (dr.Read()) {
                //取出图片对象
                byte[] newimg = (byte[])dr["ImgNew"];
                Stream stream = new MemoryStream(newimg);
                Image img = Bitmap.FromStream(stream);
                HttpContext.Current.Response.BinaryWrite(newimg);
            }
各位帮我看看这段代码有什么问题吗?
BinaryWrite(newimg)提示参数为空?

解决方案 »

  1.   

     //保存图片
        protected void Bt_Save_Click(object sender, EventArgs e)
        {        
            Stream imgStream=FU_image.PostedFile.InputStream;
            int imgLen=FU_image.PostedFile.ContentLength;
            string imgName=this.T_image.Text;
            byte[] imgBinaryData=new byte[imgLen];
            int n=imgStream.Read(imgBinaryData,0,imgLen);
        
         SqlConnection connection = new SqlConnection("Data Source=(local);Database=wxd;Uid=sa;Pwd=sa");
         SqlCommand command = new SqlCommand("insert into Image (ImageName,Image) values ( @img_name, @img_data)", connection);
     
         SqlParameter param0 = new SqlParameter("@img_name", SqlDbType.VarChar, 50);
         param0.Value = imgName;
         command.Parameters.Add(param0);
     
         SqlParameter param1 = new SqlParameter("@img_data", SqlDbType.Image);
         param1.Value = imgBinaryData;
         command.Parameters.Add(param1);
     
         connection.Open();
         int numRowsAffected = command.ExecuteNonQuery();
         connection.Close();
         this.Panel2.Visible = false;
         this.T_image.Text = "";
         bind();
        }
    取图片
     public void ProcessRequest (HttpContext context) 
        {
             string imageid = context.Request.QueryString["ImID"];
             SqlConnection connection = new SqlConnection("Data Source=(local);Database=wxd;Uid=sa;Pwd=sa");
             connection.Open();
             SqlCommand command = new SqlCommand("select [Image] from [Image] where ImageID=" + imageid, connection);
             SqlDataReader dr = command.ExecuteReader();
             dr.Read();
             context.Response.BinaryWrite((Byte[])dr[0]);
             connection.Close();
             context.Response.End();    }
      

  2.   

    (byte[])dr["ImgNew"];
    里是否有值。
      

  3.   

    楼主,问题出在保存图片的方法上。Image --> byte[] --> 保存
      

  4.   

    byte[] img ;... ...sqlcmd.Parameters.Add("@cad", SqlDbType.Image).Value = img;
      

  5.   

    对啊,我就是保存出的问题
    sqlcmd.Parameters.Add("@cad", SqlDbType.Binary).Value = img; 
    我是这样写的所以取不出图片已经改好了