C#做的WINDOW窗体程序,我想把一些图片放到SQL数据库中,怎么保存啊? 或者这样我的图片都放在D:\图片 文件夹下,想在窗体中点击 BUTTON按钮,然后自动把数据导入到数据库中,怎么办了,会的给点意见,给代码感觉不尽啊

解决方案 »

  1.   

    http://topic.csdn.net/t/20051128/10/4422308.html
    参考该贴
      

  2.   

    命令中设置参数,类型为byte[],然后将图片转换为byte[]并赋给参数,执行保存命令就可以了。
      

  3.   

    图片读取出来,就成了byte[],然后再写入表的时候使用SqlParameter,图片列的类型用Image,然后直接赋值执行就好了。
      

  4.   

    //插入图像数据
            private void InsertImage()
            {
                try
                {
                    if (null == this.picIndiPhoto.Image)                    //图像为空时, 不执行操作
                    {
                        return;
                    }
                    MemoryStream MS = new MemoryStream();
                    Bitmap bmp = new Bitmap(this.picIndiPhoto.Image);      //picIndiPhoto为Picture控件
                    bmp.Save(MS, ImageFormat.Bmp);
                    Byte[] ImageData = new Byte[MS.Length];                //将相片转为 Byte 类型数据                MS.Position = 0;
                    MS.Read(ImageData, 0, Convert.ToInt32(MS.Length));                string sql = "UPDATE t_UserInfo SET UsrPhoto=@Photo WHERE UsrID='" + this.txtUserID.Text + "'";  //SQL语句
                    SqlCommand dbCmd = new SqlCommand(sql);
                    dbCmd.CommandType = CommandType.Text;                SqlParameter parameter = new SqlParameter("@Photo", SqlDbType.Image, int.MaxValue);
                    parameter.Value = ImageData;
                    dbCmd.Parameters.Add(parameter);                SQLHelper.ExecuteNonQuery(dbCmd);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("错误信息:  " + ex.Message);
                }
            }//显示图像数据
    private void ShowImage()
    {
                string strexperssion = string.Format("SELECT UsrPhoto FROM t_UserInfo WHERE UsrID='{0}'", strUserID);  //SQL语句
                        try
                        {
                            byte[] data = (Byte[])SQLHelper.ExecuteScalar(strexperssion);
                            if (!data.Length.Equals(0))
                            {
                                MemoryStream MS = new MemoryStream(data);
                                Bitmap image = new System.Drawing.Bitmap(MS);
                                this.picIndiPhoto.Image = image;            // 从数据库里读出放在picIndiPhoto里 picIndiPhoto为Picture控件
                            }
                        }
                        catch (InvalidCastException ex)    //此处需处理空值异常
                        {
                            Console.WriteLine(ex.Message);
                        }
                        catch (Exception ec)
                        {
                            Console.WriteLine(ec.Message);
                        }
    }
      

  5.   

    片读取出来,就成了byte[],然后再写入表的时候使用SqlParameter,图片列的类型用Image,然后直接赋值执行就好了。