winform 中往数据库中添加图片的两种方式

方法一:在数据库中添加图片名字,然后把图片存在指定的文件夹中

           这种方法存起来简单,但是删除的时候麻烦。

           存:在数据库中建一个文本字段(access)或者varchar字段(sqlserver)长度能放上一张图片即可

                 过程:就是一般的存的insert into 

                          举个栗子:sql数据库中有一个id字段 自增类型 一个name字段,用于存放图片名称的类型是varchar类型

                          这winform界面中你可以拖动一个TextBox,用于存放路径,一个Button这个就不说了,还有一个openFileDialog

                         if (this.openFileDialog1.ShowDialog() == DialogResult.OK)

                          {

                               this.textBox1.Text = openFileDialog1.FileName;

                          } //打开路径,必不可少的     这些都是是在button单击事件里完成的

                          if (openFileDialog1.FileName.Length > 0) // 判断openFileDialog1路径的长度

                           {

                                  string oldName = openFileDialog1.FileName;            //定义一个string类型的变量 用于存放【文件路径】

                                   string[] splitName = oldName.Split('.');                     //为获取文件的扩展名做准备的

                                   string ext = splitName[splitName.Length - 1];          //文件的扩展名

                                    if (ext == "jpg" || ext == "gif" || ext == "bmp" || ext == "JPG")   //限制上传图片的格式

                                    {

                                         string dbName = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + ext; //给上传的图片起个名字!以时间命名

                                         string newName = AppDomain.CurrentDomain.BaseDirectory  + dbName;  //新路径!这是个相对路径

                                         File.Copy(oldName, newName, true);  //把文件从以前的路径复制到新的路径中去

                                         //下面就开始添加到数据库里面了

                                         string constring="";//数据库的连接字符串

                                         using (SqlConnection con=new SqlConnection (constring))

                                          {

                                              con.open();

                                              stirng sql="insert into shujukuname (name) values(@name)"; //因为id自增的这里只需要添加图片名称

                                              SqlCommand cmd = new SqlCommand(sql,con);

                                              cmd.Parameters.Add("@name",SqlDbType.VarChar).Value=TextBox1.Text.Trim();

                                              cmd.ExecuteNonQuery();

                                              con.Close();

                                          }

                                    }                     

                           }                                                                                              //这样就添加完成了

            显示图片:  这里一定要有一个PictureBox1

                          要从数据库中取出这个name

                         using (SqlConnection con=new SqlConnection (constring))

                         {

                              string sql="select name from shujukuname where id=' 1'   ";//当id=1的时候查询的图片的名称

                               SqlDataAdapter da = new SqlDataAdapter(sql, con);

                               con.Open();

                               DataTable dt=new  DataTable();

                               da.Fill(dt);

                               string name =dt.Rows[0]["name"].ToString();

                               con.Close();                          //这样就获取出name来了,然后进行显示

                         }

                               string path=Application.StartupPath + "//"+ name;  //一张图片的路径

                               if(File.Exists(path)) //根据这个路劲显示有没有这张图片

                               {

                                      PictureBox1.Image=Image.FromFile(path);                  //PictureBox1显示的path路径的图片

                               }

                              else

                                {

                                        PictureBox1.Image=Image.FromFile(Application.StartupPath + "//" +默认图片);  //PictureBox1显示的默认的的图片

                                }

winform 中往数据库中添加图片的两种方式之二:直接把图片添加在sqlserver中

首先要有一个image或者binary类型的字段

这种是以二进制形式插入到数据库中

                 FileStream fs = new FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                 byte[] buffByte = new byte[fs.Length];
                 fs.Read(buffByte, 0, (int)fs.Length);
                 fs.Close();                                        //数据库字段为image类型,将图片转化为byte[],保存到数据库
      
                 SqlConnection   db   =   new   SqlConnection(strConn); 
                 db.Open(); 

                 string   strSQL   =   "INSERT   INTO   shujuktable (name)   values   (@name) "; 
                 SqlCommand   cmd   =   new   SqlCommand(strSQL,   db); 
                 cmd.Parameters.Add( "@name",   SqlDbType.Image); 
                 cmd.Parameters[ "@name"].Value   =   buffByte

                 cmd.ExecuteNonQuery();                                       //保存图片的过程

清理资源

以上这部分是如何添加图片,下面这部分是读取图片

-————————————————————————————————————————————————

             SqlConnection conn = new SqlConnection(strConn);


             DataTable dt = new DataTable();
             SqlDataAdapter adp = new SqlDataAdapter("select name from shujuktable where ID='1'", conn);
             adp.Fill(dt);
             byte[] buffByte = (byte[])dt.Rows[0][“name”];

             MemoryStream ms = new MemoryStream(buffByte);
             Image image = Image.FromStream(ms, true);

             this.pictureEdit1.Image = image;

posted on 2011-09-08 10:01 SkyDream 阅读(3245) 评论(0)  编辑  收藏 所属分类: ASP.NET

<2011年9月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜