具体要求如下:
在窗体中通过打开文件对话框选择计算机磁盘上的一个图片,然后这个图片可以在窗体的Image控件中显示,最后再通过单击一个“确定”按钮后可以把这张图片存入输入库sql server不知哪位高手有这个源码共享一下,谢谢

解决方案 »

  1.   

    private void btnsave_Click(object sender, System.EventArgs e)
            {            
                Stream ImageStream;
                string Path=File1.PostedFile.FileName;// 文件名称
                int Size = File1.PostedFile.ContentLength; // 文件大小
                string Type = File1.PostedFile.ContentType; // 文件类型
                ImageStream = File1.PostedFile.InputStream;
                byte[] Content = new byte[Size];
                int Status = ImageStream.Read(Content, 0, Size);            // 写入数据库
                SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
                SqlCommand comm=new SqlCommand("insert into testimage (UserName,Image,Path,Type) values(@UserName,@Image,@Path,@Type)",conn);            comm.CommandType = CommandType.Text;
                comm.Parameters.Add("@UserName", SqlDbType.VarChar, 255).Value = txtUserName.Text;            
                comm.Parameters.Add("@Image", SqlDbType.Image).Value = Content;
                comm.Parameters.Add("@Path", SqlDbType.VarChar, 255).Value = Path;
                comm.Parameters.Add("@Type", SqlDbType.VarChar, 255).Value = Type;            conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
                DataBind();
            }http://singlepine.cnblogs.com/archive/2005/11/30/288027.html
      

  2.   

    谢谢小山兄弟的回答,写入数据库的代码完全可以,不过在winform中没有webform中的file控件。不知在winform中如何实现象file这样的效果的代码?
      

  3.   

    使用openfiledialog//
    string txt=this.ofdLoadPhoto.FileName;
    FileStream fs=File.OpenRead(txt);
    byte[] content=new byte[fs.Length];
    fs.Read(content, 0,content.Length);
    fs.Close();
    string sql ="update xs_ksxx set zp=@pic where xjh='"+this.txtXJH.Text.Trim()+"'";
    SqlCommand cmd=new SqlCommand(sql,con);
    cmd.CommandType=CommandType.Text;
    cmd.Parameters.Add("@pic",SqlDbType.Image).Value=content;
    cmd.ExecuteNonQuery();
    cmd.Dispose();
      

  4.   

    转为byte类型后存入Image字段。
    byte[] imagebytes=null;
    FileStream fs=new FileStream(Image_path,FileMode.Open);
    BinaryReader br=new BinaryReader(fs);
    imagebytes=br.ReadBytes(br.Length);
    SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
    parInput22.Direction=ParameterDirection.Input;
    cmd.Parameters["@员工图片"].Value=imagebytes;
    cmd.ExecuteNonQuery();
    数据库中操作图片
    How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
    DataGrid显示图片(物理路径式和Stream流式)和添加图片到数据库
    http://singlepine.cnblogs.com/articles/288027.html
      

  5.   

    显示图片:
    if(myReader["studentPhoto"].ToString() != "") // 如果图片字段不为空时显示图片数据
    {
    imagebytes = (byte[]) (myReader["studentPhoto"]);
    studentAbout.Text = myReader["studentAbout"].ToString(); // 预先显示一下附注信息,免得数据库图片字段有值,但不是图片时产生异常而不执行下面的内容。
    if (imagebytes.Length > 0)
    {
    MemoryStream stream = new MemoryStream(imagebytes, true); // 创建一个内存流,支持写入,用于存放图片二进制数据
    try
    {
    stream.Write(imagebytes, 0, imagebytes.Length);
    Bitmap FinalImage = new Bitmap(stream);
    this.studentPhoto.Image = FinalImage;
    this.studentPhoto.Refresh();
    }
    finally
    {
    stream.Close(); 
    }
    }
    }
    cmd.Parameters.Add("@studentPhoto", SqlDbType.Image).Value = imagebytes;