码迷,mamicode.com
首页 > 数据库 > 详细

从sql中image类型字段中导出图片

时间:2015-03-14 18:11:15      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing;

namespace 获取名字
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\Users\Chinaese\Desktop\_FabricImg";
            string x = Path.GetFileName(Path.GetDirectoryName(path));

            DirectoryInfo root = new DirectoryInfo(path);
            // DirectoryInfo[] childDirs = root.GetDirectories();
            FileInfo[] files = root.GetFiles();
            List<string> filename = new List<string>();
            foreach (FileInfo item in files)
            {
                filename.Add(Path.GetFileNameWithoutExtension(item.FullName));
            }

    
           string connectionString=@"Data Source = 192.168.0.180; Initial Catalog = pms; User Id = sa; Password = hualong2012;";
            using (SqlConnection connection = 
             new SqlConnection(connectionString)) 
            { 
             SqlCommand command =
             new SqlCommand("SELECT FlowerCode,PicImg FROM FlowerPics where flowerid  IN ( SELECT     MAX(flowerid) AS Expr1 FROM         FlowerPics GROUP BY FlowerCode )", connection); 
             connection.Open(); 
 
             SqlDataReader reader = command.ExecuteReader(); 
 
             // 判断数据是否读到尾. 
             while (reader.Read()) 
             {
                 //声明数组,用于保存数据库的二进制数据
                 byte[] mybyte = null;
                 //读取数据保存到数组中
                 try
                 {
                     if (filename.Contains(reader[0]))
                     {
                         continue;
                     }
                     mybyte = (byte[])reader["PicImg"];
                     Image image;
                     //读取数组数据成为文件流
                     MemoryStream mymemorystream = new MemoryStream(mybyte);
                     //转换成为图片格式。
                     image = Image.FromStream(mymemorystream, true);
                    
           
                     //image.Save(mymemorystream, ImageFormat.Jpeg);
                     image.Save(@"C:\Users\Chinaese\Desktop\SAMP\" + reader[0].ToString() + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                     mymemorystream.Close();  //关闭流
                     filename.Add(reader[0].ToString());

                 }
                 catch (Exception)
                 {
                    
                     //break;
                 }
                 finally
                 {

                 }

                 //Console.WriteLine(String.Format("{0}, {1}",      reader[0], reader[1])); 
             } 
 
             // 一定要关闭 reader 对象. 
             reader.Close(); 
            }



            Console.ReadKey();


        }
    }
}

  

从sql中image类型字段中导出图片

标签:

原文地址:http://www.cnblogs.com/ChineseMoonGod/p/4337804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!