从文件中加载二进制图像数据

// 从文件中加载二进制图像数据
// LoadImages.cs
// 在C#程序中,二进制数据类型映射为字节数据(byet[]),
// 字符数据类型映射为字符串或者字符数组(char[])

using   System;
using   System.Data;
using   System.Data.SqlClient;
using   System.IO;

namespace   Ch18
{
      class   LoadImages
    {
          // 假设图片文件使用 image1.jpg、image2.jpg..imageX.jpg命名规则存放在当前目录的pic目录下
          string   imgPath =   ". \\pic\\ "   ;       
          string   imgPrefix =   "image" ;
          int   fileNumber = 3;
          string   imgType =   ".jpg" ;
          int   maxImageSize = 10000;
        SqlConnection conn =   null ;
        SqlCommand cmd =   null ;
          static   void   Main()
        {
            LoadImages loader =   new   LoadImages();
              try
            {
                loader.OpenConnection();
                loader.CreateCommand();
                loader.CreateImageTable();
                loader.PrepareInsertImages();
                  int   i;
                  for   (i = 1; i <= loader.fileNumber; i++)
                {
                    loader.ExecuteInsertImages(i);
                }
            }
              catch (SqlException ex)
            {
                  Console .WriteLine(ex.ToString());
            }
              finally
            {
                loader.CloseConnection();
                Console.WriteLine("博客园首发");
                  Console .ReadLine();
            }
        }

          void   OpenConnection()
        {
              string   strConn =   "server=.\\MSSQL2012;integrated security = true; database=tempdb" ;
            conn =   new   SqlConnection(strConn);
            conn.Open();
        }

          void   CloseConnection()
        {
            conn.Close();
              Console .WriteLine(   "连接关闭"   );
        }

          void   CreateCommand()
        {
            cmd =   new   SqlCommand();
            cmd.Connection = conn;
        }

          void   ExecuteCommand(   string   cmdText)
        {
              int   cmdResult;
            cmd.CommandText = cmdText;
              Console .WriteLine(   "执行SQL:" );
              Console .WriteLine(cmd.CommandText);
            cmdResult = cmd.ExecuteNonQuery();
        }

          void   CreateImageTable()
        {
            ExecuteCommand(   @"create table imagetable(imagefile nvarchar(20), imagedata varbinary(max))" );
        }

          void   PrepareInsertImages()
        {
            cmd.CommandText =   @"insert into imagetable values(@file,@data)" ;
            cmd.Parameters.Add(   "@file" , SqlDbType.NVarChar, 20);
            cmd.Parameters.Add(   "@data" , SqlDbType.Image, 1000000);
            cmd.Prepare();
        }

          void   ExecuteInsertImages(   int   fileNo)
        {
              string   strFile =   null ;
              byte [] imgData =   null ;
            strFile = imgPrefix + fileNo.ToString() + imgType;
            imgData = LoadImageFile(strFile, imgPath, maxImageSize);
            cmd.Parameters[   "@file" ].Value = strFile;
            cmd.Parameters[   "@data" ].Value = imgData;
            ExecuteCommand(cmd.CommandText);
        }

          byte [] LoadImageFile(   string   strFile,   string   strPath,   int   iSize)
        {
              byte [] imageBytes =   null ;
              string   strFullPath = strPath + strFile;
              Console .WriteLine(   "加载文件:"   );
              Console .WriteLine(strFullPath);
              FileStream   fs =   new   FileStream (strFullPath,   FileMode .Open,   FileAccess .Read);
              BinaryReader   br =   new   BinaryReader   (fs);
            imageBytes = br.ReadBytes(iSize);
              Console .WriteLine(   "Imagebytes has length {0} bytes."   , imageBytes.GetLength(0));
              return   imageBytes;
        }
    }
}
------------
执行SQL:
create table imagetable(imagefile nvarchar(20), imagedata varbinary(max))
加载文件:
.\pic\image1.jpg
Imagebytes has length 10000 bytes.
执行SQL:
insert into imagetable values(@file,@data)
加载文件:
.\pic\image2.jpg
Imagebytes has length 10000 bytes.
执行SQL:
insert into imagetable values(@file,@data)
加载文件:
.\pic\image3.jpg
Imagebytes has length 10000 bytes.
执行SQL:
insert into imagetable values(@file,@data)
连接关闭




转载于:https://www.cnblogs.com/bloodfist/p/3281648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值