 
                    
                
         
    
    
    
    
    
        
            
                
    
        
            
            
            
                    
        
        ...
    
![]() using System;
using System;
![]() using System.Data;
using System.Data;
![]() using System.Text;
using System.Text;
![]() using System.Windows.Forms;
using System.Windows.Forms;
![]() using System.Configuration;
using System.Configuration;
![]() using System.Data.SqlClient;
using System.Data.SqlClient;
![]() using System.IO;
using System.IO;
![]()
![]() namespace LobTest
namespace LobTest
![]()
![]()
![]() {
{
![]() public partial class Form1 : Form
   public partial class Form1 : Form
![]()
![]() 
   ![]() {
{
![]() public Form1()
      public Form1()
![]()
![]() 
      ![]() {
{
![]() InitializeComponent();
         InitializeComponent();
![]() }
      }
![]()
![]() private void button1_Click(object sender, EventArgs e)//读取BLOB数据并写入文件模式
      private void button1_Click(object sender, EventArgs e)//读取BLOB数据并写入文件模式
![]()
![]() 
      ![]() {
{
![]() const int employeeIdColumn = 0;
         const int employeeIdColumn = 0;
![]() const int employeePhotoColumn = 1;
         const int employeePhotoColumn = 1;
![]() //bufferSize must be bigger than oleOffset
         //bufferSize must be bigger than oleOffset
![]() const int bufferSize = 100;
         const int bufferSize = 100;
![]() byte[] buffer = new byte[bufferSize];
         byte[] buffer = new byte[bufferSize];
![]() int byteCountRead;
         int byteCountRead;
![]() long currentIndex = 0;
         long currentIndex = 0;
![]()
![]() ConnectionStringSettings nwSetting =
         ConnectionStringSettings nwSetting =
![]() ConfigurationManager.ConnectionStrings["NwString"];
            ConfigurationManager.ConnectionStrings["NwString"];
![]() using (SqlConnection cn = new SqlConnection())
         using (SqlConnection cn = new SqlConnection())
![]()
![]() 
         ![]() {
{
![]() cn.ConnectionString = nwSetting.ConnectionString;
            cn.ConnectionString = nwSetting.ConnectionString;
![]() cn.Open();
            cn.Open();
![]()
![]() using (SqlCommand cmd = cn.CreateCommand())
            using (SqlCommand cmd = cn.CreateCommand())
![]()
![]() 
            ![]() {
{
![]() cmd.CommandText =
               cmd.CommandText =
![]() "SELECT EmployeeID, Photo FROM Employees";
                  "SELECT EmployeeID, Photo FROM Employees";
![]() SqlDataReader rdr = cmd.ExecuteReader(
               SqlDataReader rdr = cmd.ExecuteReader(
![]() CommandBehavior.SequentialAccess);
                  CommandBehavior.SequentialAccess);
![]() while (rdr.Read())
               while (rdr.Read())
![]()
![]() 
               ![]() {
{
![]() int employeeId =
                  int employeeId =
![]() rdr.GetInt32(employeeIdColumn);
                     rdr.GetInt32(employeeIdColumn);
![]() string fileName = @"c:\Employee"
                  string fileName = @"c:\Employee"
![]() + employeeId.ToString().PadLeft(2, '0')
                     + employeeId.ToString().PadLeft(2, '0')
![]() + ".bin";
                     + ".bin";
![]()
![]() // Create a file to hold the output.
                  // Create a file to hold the output.
![]() using (FileStream fs = new FileStream(
                  using (FileStream fs = new FileStream(
![]() fileName, FileMode.OpenOrCreate,
                     fileName, FileMode.OpenOrCreate,
![]() FileAccess.Write))
                     FileAccess.Write))
![]()
![]() 
                  ![]() {
{
![]() currentIndex = 0;
                     currentIndex = 0;
![]() byteCountRead =
                     byteCountRead =
![]() (int)rdr.GetBytes(employeePhotoColumn,
                       (int)rdr.GetBytes(employeePhotoColumn,
![]() currentIndex, buffer, 0, bufferSize);
                       currentIndex, buffer, 0, bufferSize);
![]() while (byteCountRead != 0)
                     while (byteCountRead != 0)
![]()
![]() 
                     ![]() {
{
![]() fs.Write(buffer, 0, byteCountRead);
                        fs.Write(buffer, 0, byteCountRead);
![]() currentIndex += byteCountRead;
                        currentIndex += byteCountRead;
![]() byteCountRead =
                        byteCountRead =
![]() (int)rdr.GetBytes(employeePhotoColumn,
                          (int)rdr.GetBytes(employeePhotoColumn,
![]() currentIndex, buffer, 0, bufferSize);
                          currentIndex, buffer, 0, bufferSize);
![]() }
                     }
![]() }
                  }
![]() }
               }
![]() }
            }
![]() }
         }
![]() MessageBox.Show("Done");
         MessageBox.Show("Done");
![]() }
      }
![]()
![]() private void button2_Click(object sender, EventArgs e)//读取BLOB数据并写入文件模式删除OLE头信息
       private void button2_Click(object sender, EventArgs e)//读取BLOB数据并写入文件模式删除OLE头信息
![]()
![]() 
      ![]() {
{
![]() const int oleOffset = 78;
         const int oleOffset = 78;
![]() const int oleTypeStart = 20;
         const int oleTypeStart = 20;
![]() const int oleTypeLength = 12;
         const int oleTypeLength = 12;
![]() const int employeeIdColumn = 0;
         const int employeeIdColumn = 0;
![]() const int employeePhotoColumn = 1;
         const int employeePhotoColumn = 1;
![]() const int bufferSize = 100; //must be bigger than oleOffset
         const int bufferSize = 100; //must be bigger than oleOffset
![]() byte[] buffer = new byte[bufferSize];
         byte[] buffer = new byte[bufferSize];
![]() int bufferStart = 0;
         int bufferStart = 0;
![]() int byteCountRead;
         int byteCountRead;
![]() long currentIndex = 0;
         long currentIndex = 0;
![]()
![]() ConnectionStringSettings nwSetting =
         ConnectionStringSettings nwSetting =
![]() ConfigurationManager.ConnectionStrings["NwString"];
            ConfigurationManager.ConnectionStrings["NwString"];
![]() using (SqlConnection cn = new SqlConnection())
         using (SqlConnection cn = new SqlConnection())
![]()
![]() 
         ![]() {
{
![]() cn.ConnectionString = nwSetting.ConnectionString;
            cn.ConnectionString = nwSetting.ConnectionString;
![]() cn.Open();
            cn.Open();
![]()
![]() using (SqlCommand cmd = cn.CreateCommand())
            using (SqlCommand cmd = cn.CreateCommand())
![]()
![]() 
            ![]() {
{
![]() cmd.CommandText =
               cmd.CommandText =
![]() "SELECT EmployeeID, Photo FROM Employees";
                  "SELECT EmployeeID, Photo FROM Employees";
![]() SqlDataReader rdr = cmd.ExecuteReader(
               SqlDataReader rdr = cmd.ExecuteReader(
![]() CommandBehavior.SequentialAccess);
                  CommandBehavior.SequentialAccess);
![]() while (rdr.Read())
               while (rdr.Read())
![]()
![]() 
               ![]() {
{
![]() int employeeId = rdr.GetInt32(employeeIdColumn);
                  int employeeId = rdr.GetInt32(employeeIdColumn);
![]() string fileName = @"c:\Employee" +
                  string fileName = @"c:\Employee" +
![]() employeeId.ToString().PadLeft(2, '0') + ".bmp";
                     employeeId.ToString().PadLeft(2, '0') + ".bmp";
![]()
![]() // Create a file to hold the output.
                  // Create a file to hold the output.
![]() using (FileStream fs = new FileStream(
                  using (FileStream fs = new FileStream(
![]() fileName, FileMode.OpenOrCreate,
                     fileName, FileMode.OpenOrCreate,
![]() FileAccess.Write))
                     FileAccess.Write))
![]()
![]() 
                  ![]() {
{
![]() currentIndex = 0;
                     currentIndex = 0;
![]() //read until we have the oleheader, if possible
                     //read until we have the oleheader, if possible
![]() while (currentIndex < oleOffset)
                     while (currentIndex < oleOffset)
![]()
![]() 
                     ![]() {
{
![]() byteCountRead =
                        byteCountRead =
![]() (int)rdr.GetBytes(employeePhotoColumn,
                           (int)rdr.GetBytes(employeePhotoColumn,
![]() currentIndex, buffer, (int)currentIndex,
                           currentIndex, buffer, (int)currentIndex,
![]() bufferSize - (int)currentIndex);
                           bufferSize - (int)currentIndex);
![]() if (byteCountRead == 0) break;
                        if (byteCountRead == 0) break;
![]() currentIndex += byteCountRead;
                        currentIndex += byteCountRead;
![]() }
                     }
![]() byteCountRead = (int)currentIndex;
                     byteCountRead = (int)currentIndex;
![]()
![]() //process oleheader, if it exists
                     //process oleheader, if it exists
![]() if (byteCountRead >= oleOffset)
                     if (byteCountRead >= oleOffset)
![]()
![]() 
                     ![]() {
{
![]() string type = Encoding.ASCII.GetString(
                        string type = Encoding.ASCII.GetString(
![]() buffer, oleTypeStart, oleTypeLength);
                           buffer, oleTypeStart, oleTypeLength);
![]() if (type == "Bitmap Image")
                        if (type == "Bitmap Image")
![]()
![]() 
                        ![]() {
{
![]() bufferStart = oleOffset;
                           bufferStart = oleOffset;
![]() byteCountRead = byteCountRead - oleOffset;
                           byteCountRead = byteCountRead - oleOffset;
![]() }
                        }
![]() }
                     }
![]()
![]() while (byteCountRead != 0)
                     while (byteCountRead != 0)
![]()
![]() 
                     ![]() {
{
![]() fs.Write(buffer, bufferStart, byteCountRead);
                        fs.Write(buffer, bufferStart, byteCountRead);
![]() bufferStart = 0;
                        bufferStart = 0;
![]() byteCountRead =
                        byteCountRead =
![]() (int)rdr.GetBytes(employeePhotoColumn,
                           (int)rdr.GetBytes(employeePhotoColumn,
![]() currentIndex, buffer, 0, bufferSize);
                           currentIndex, buffer, 0, bufferSize);
![]() currentIndex += byteCountRead;
                        currentIndex += byteCountRead;
![]() }
                     }
![]() }
                  }
![]() }
               }
![]() }
            }
![]() }
         }
![]() MessageBox.Show("Done");
         MessageBox.Show("Done");
![]() }
      }
![]()
![]() private void button3_Click(object sender, EventArgs e)//写如BLOB数据
      private void button3_Click(object sender, EventArgs e)//写如BLOB数据
![]()
![]() 
      ![]() {
{
![]() const int bufferSize = 100;
         const int bufferSize = 100;
![]() byte[] buffer = new byte[bufferSize];
         byte[] buffer = new byte[bufferSize];
![]() long currentIndex = 0;
         long currentIndex = 0;
![]() byte[] photoPtr;
         byte[] photoPtr;
![]()
![]() ConnectionStringSettings nwSetting =
         ConnectionStringSettings nwSetting =
![]() ConfigurationManager.ConnectionStrings["NwString"];
            ConfigurationManager.ConnectionStrings["NwString"];
![]() using (SqlConnection cn = new SqlConnection())
         using (SqlConnection cn = new SqlConnection())
![]()
![]() 
         ![]() {
{
![]() cn.ConnectionString = nwSetting.ConnectionString;
            cn.ConnectionString = nwSetting.ConnectionString;
![]() cn.Open();
            cn.Open();
![]() using (SqlCommand cmd = cn.CreateCommand())
            using (SqlCommand cmd = cn.CreateCommand())
![]()
![]() 
            ![]() {
{
![]() cmd.CommandText =
               cmd.CommandText =
![]() "SELECT TEXTPTR(Photo) FROM Employees WHERE EmployeeID = 1";
        "SELECT TEXTPTR(Photo) FROM Employees WHERE EmployeeID = 1";
![]() photoPtr = (byte[])cmd.ExecuteScalar();
               photoPtr = (byte[])cmd.ExecuteScalar();
![]() }
            }
![]() using (SqlCommand cmd = cn.CreateCommand())
            using (SqlCommand cmd = cn.CreateCommand())
![]()
![]() 
            ![]() {
{
![]() cmd.CommandText =
               cmd.CommandText =
![]() "UPDATETEXT Employees.Photo @Pointer @Offset null @Data";
        "UPDATETEXT Employees.Photo @Pointer @Offset null @Data";
![]() SqlParameter ptrParm =
               SqlParameter ptrParm =
![]() cmd.Parameters.Add("@Pointer", SqlDbType.Binary, 16);
                  cmd.Parameters.Add("@Pointer", SqlDbType.Binary, 16);
![]() ptrParm.Value = photoPtr;
               ptrParm.Value = photoPtr;
![]() SqlParameter photoParm =
               SqlParameter photoParm =
![]() cmd.Parameters.Add("@Data", SqlDbType.Image);
                  cmd.Parameters.Add("@Data", SqlDbType.Image);
![]() SqlParameter offsetParm =
               SqlParameter offsetParm =
![]() cmd.Parameters.Add("@Offset", SqlDbType.Int);
                  cmd.Parameters.Add("@Offset", SqlDbType.Int);
![]() offsetParm.Value = 0;
               offsetParm.Value = 0;
![]() using (FileStream fs = new FileStream("Girl.gif",
               using (FileStream fs = new FileStream("Girl.gif",
![]() FileMode.Open, FileAccess.Read))
                  FileMode.Open, FileAccess.Read))
![]()
![]() 
               ![]() {
{
![]() int count = fs.Read(buffer, 0, bufferSize);
                  int count = fs.Read(buffer, 0, bufferSize);
![]() while (count != 0)
                  while (count != 0)
![]()
![]() 
                  ![]() {
{
![]() photoParm.Value = buffer;
                     photoParm.Value = buffer;
![]() photoParm.Size = count;
                     photoParm.Size = count;
![]() cmd.ExecuteNonQuery();
                     cmd.ExecuteNonQuery();
![]() currentIndex += count;
                     currentIndex += count;
![]() offsetParm.Value = currentIndex;
                     offsetParm.Value = currentIndex;
![]() count = fs.Read(buffer, 0, bufferSize);
                     count = fs.Read(buffer, 0, bufferSize);
![]() }
                  }
![]() }
               }
![]() }
            }
![]() }
         }
![]() MessageBox.Show("Done");
         MessageBox.Show("Done");
![]() }
      }
![]() }
   }
![]() }
} 
             
            posted @ 
2007-08-25 11:13 
子午 
阅读(
303) 
评论() 
 
收藏 
举报