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 + "http://"+ name;  //一張圖片的路徑

                                         if(File.Exists(path)) //根據這個路勁顯示有沒有這張圖片

                                         {

                                                PictureBox1.Image=Image.FromFile(path);                  //PictureBox1顯示的path路徑的圖片

                                         }

                                        else

                                          {

                                                  PictureBox1.Image=Image.FromFile(Application.StartupPath + "http://" +默認圖片);  //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 閱讀(3286) 評論(0)  編輯  收藏 所屬分類: ASP.NET

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

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 昔阳县| 钦州市| 洛川县| 东明县| 博野县| 房产| 甘南县| 乐东| 衡水市| 英超| 宝鸡市| 云浮市| 衡山县| 通榆县| 深水埗区| 炉霍县| 南京市| 封丘县| 寻甸| 正定县| 宁化县| 曲阜市| 工布江达县| 湖南省| 毕节市| 汽车| 阳城县| 灵石县| 界首市| 封丘县| 龙海市| 乌拉特前旗| 北票市| 邯郸市| 天峻县| 招远市| 平谷区| 资兴市| 连江县| 同仁县| 宜阳县|