C#读取大文件(三)

sqlcmd.CommandText = strSelect;    
InBlock.gif              sqlcmd.ExecuteNonQuery();            
InBlock.gif             }    
InBlock.gif          }    
InBlock.gif         }    
InBlock.gif         bytContent =  null ;    
InBlock.gif         fs.Close();    
InBlock.gif      }    
InBlock.gif       catch     
InBlock.gif      {    
InBlock.gif         state = 0;    
InBlock.gif         bytContent =  null ;    
InBlock.gif         fs.Close();    
InBlock.gif          return   false ;    
InBlock.gif      }    
InBlock.gif      state = 0;    
InBlock.gif       return   true ;    
InBlock.gif     }    
InBlock.gif  
InBlock.gif                  /// <summary>    
InBlock.gif                  /// 从数据库读取word    
InBlock.gif                  /// </summary>    
InBlock.gif                  /// <param name="sqlcon">sql连接类</param>    
InBlock.gif                  /// <param name="strTargetSelect">直接获取目标内容的sql,"select 目标列名 from 目标表名 where 条件"</param>    
InBlock.gif                  /// <param name="strTargetLength">获取目标内容的长度的sql,"select datalength(目标列名) from 目标表名 where 条件"</param>    
InBlock.gif                  /// <param name="strTargetHandle">获取目标内容的句柄的sql,"select @content=textptr(目标列名) from 目标表名 where 条件"</param>    
InBlock.gif                  /// <param name="strTableName">目标表名</param>    
InBlock.gif                  /// <param name="strColumnName">目标列名</param>    
InBlock.gif                  /// <param name="strPath">要导出并保存的word的路径</param>    
InBlock.gif                  /// <param name="intSetBlock">定义块大小</param>    
InBlock.gif      /// <param name="bolShowDialog">是否显示进度条</param>    
InBlock.gif      /// <param name="strFormName">窗体名称</param>    
InBlock.gif      /// <param name="ico">窗体图标</param>    
InBlock.gif      /// <param name="strShowContent">显示内容</param>    
InBlock.gif                  /// <returns>true表成功</returns>    
InBlock.gif      public   bool  ReadWordDocument(System.Data.SqlClient.SqlConnection sqlcon, string  strTargetSelect, string  strTargetLength, string  strTargetHandle, string  strTableName, string  strColumnName, string  strPath, int  intSetBlock, bool  bolShowDialog, string  strFormName,System.Drawing.Icon ico, string  strShowContent)    
InBlock.gif {    
InBlock.gif       //初始化SqlCommand    
InBlock.gif                         System.Data.SqlClient.SqlCommand sqlcmd =  new  System.Data.SqlClient.SqlCommand();    
InBlock.gif                         sqlcmd.Connection = sqlcon;    
InBlock.gif      sqlcmd.CommandType = System.Data.CommandType.Text;    
InBlock.gif  
InBlock.gif       int  intBlock = intSetBlock;  //块大小    
InBlock.gif       int  intCount = 0;  //分快数    
InBlock.gif       int  intLength = 0;  //获取的image列中的内容的长度    
InBlock.gif       string  strSelect = "";  //要执行的sql查询语句    
InBlock.gif       byte  []bytContent =  null //定义内容数组    
InBlock.gif       int  intPercent = 0;  //获取读取的比例    
InBlock.gif       //建立要输出的文件流    
InBlock.gif      System.IO.FileStream fs =  new  FileStream(strPath,System.IO.FileMode.Create);    
InBlock.gif  
InBlock.gif       try     
InBlock.gif      {    
InBlock.gif          //获取指定image列中的内容长度    
InBlock.gif         sqlcmd.CommandText = strTargetLength;    
InBlock.gif         intLength = ( int )sqlcmd.ExecuteScalar();    
InBlock.gif          //如果长度为0    
InBlock.gif          if (intLength == 0)    
InBlock.gif         {    
InBlock.gif           return   false ;    
InBlock.gif         }    
InBlock.gif          //获得分快数    
InBlock.gif         intCount = intLength / intBlock;    
InBlock.gif  
InBlock.gif                                  //是否显示精度    
InBlock.gif          if (bolShowDialog)    
InBlock.gif         {    
InBlock.gif           //获取精度窗体,引用窗体中的进度条和按钮控件    
InBlock.gif          System.Windows.Forms.Form frmProgress = getDialog(strFormName,ico,strShowContent);    
InBlock.gif          System.Windows.Forms.ProgressBar prgLoader =  null ;    
InBlock.gif          System.Windows.Forms.Button btnOk =  null ;    
InBlock.gif          System.Windows.Forms.Label lblShowPercent =  null ;    
InBlock.gif           foreach (System.Windows.Forms.Control control  in  frmProgress.Controls)    
InBlock.gif          {    
InBlock.gif              if (control.GetType().ToString() ==  "System.Windows.Forms.ProgressBar" )    
InBlock.gif             {    
InBlock.gif              prgLoader = (System.Windows.Forms.ProgressBar)control;    
InBlock.gif             }    
InBlock.gif              if (control.GetType().ToString() ==  "System.Windows.Forms.Button" )    
InBlock.gif             {    
InBlock.gif              btnOk = (System.Windows.Forms.Button)control;    
InBlock.gif  }    
InBlock.gif              if (control.GetType().ToString() ==  "System.Windows.Forms.Label"  && control.Name ==  "lblShowPercent" )    
InBlock.gif             {    
InBlock.gif              lblShowPercent = (System.Windows.Forms.Label)control;    
InBlock.gif             }    
InBlock.gif          }    
InBlock.gif              
InBlock.gif          frmProgress.Show();    
InBlock.gif           //启动转换    
InBlock.gif          state = 1;    
InBlock.gif           if (intCount == 0)    
InBlock.gif          {    
InBlock.gif             strSelect = strTargetSelect;    
InBlock.gif             sqlcmd.CommandText = strSelect;    
InBlock.gif             bytContent =  new   byte [intLength];    
InBlock.gif             bytContent = sqlcmd.ExecuteScalar()  as   byte [];    
InBlock.gif             fs.Write(bytContent,0,intLength);    
InBlock.gif             prgLoader.Value = 100;    
InBlock.gif             lblShowPercent.Text = prgLoader.Value +  "%" ;    
InBlock.gif             btnOk.Text =  "关闭" ;    
InBlock.gif                 
InBlock.gif          }    
InBlock.gif           else     
InBlock.gif          {    
InBlock.gif              int  i = 0;    
InBlock.gif             bytContent =  new   byte [intBlock];    
InBlock.gif              while (i < intCount)    
InBlock.gif             {    
InBlock.gif               if (state == 0)    
InBlock.gif              {    
InBlock.gif                 bytContent =  null ;    
InBlock.gif                 System.IO.File.Delete(strPath);    
InBlock.gif                 fs.Close();    
InBlock.gif                  return   false ;    
InBlock.gif              }    
InBlock.gif              strSelect =  "declare @content varbinary(16) " //再sql中声明获取目标image列内容的句柄变量    
InBlock.gif              strSelect += strTargetHandle;  //获取句柄    
InBlock.gif               //锁定并读取指定长度的数据    
InBlock.gif              strSelect +=  " readtext "  + strTableName +  "."  + strColumnName +  " @content @start @count HOLDLOCK" ;    
InBlock.gif               if (i == 0)    
InBlock.gif              {    
InBlock.gif                  //添加@start和@count变量,分别表偏移变量和取的长度    
InBlock.gif  sqlcmd.Parameters.Add( "@start" ,System.Data.SqlDbType.Int).Value = 0;     
InBlock.gif                 sqlcmd.Parameters.Add( "@count" ,System.Data.SqlDbType.Int).Value = intBlock;    
InBlock.gif              }    
InBlock.gif               else     
InBlock.gif              {    
InBlock.gif                 sqlcmd.Parameters[ "@start" ].Value = i * intBlock;    
InBlock.gif                 sqlcmd.Parameters[ "@count" ].Value = intBlock;    
InBlock.gif              }    
InBlock.gif              sqlcmd.CommandText = strSelect;    
InBlock.gif              bytContent = sqlcmd.ExecuteScalar()  as   byte [];    
InBlock.gif              fs.Write(bytContent,0,intBlock);    
InBlock.gif              intPercent = ( int )((( double )(i * intBlock)) / ( double )(intLength) * 100);    
InBlock.gif              prgLoader.Value = intPercent;    
InBlock.gif              lblShowPercent.Text = prgLoader.Value.ToString() +  "%" ;    
InBlock.gif              Application.DoEvents();    
InBlock.gif              ++i;    
InBlock.gif             }    
InBlock.gif              //将剩余的字节写入流    
InBlock.gif              int  intResidual = intLength % intBlock;    
InBlock.gif              if (intResidual > 0)    
InBlock.gif             {    
InBlock.gif              strSelect =  "declare @content varbinary(16) " //再sql中声明获取目标image列内容的句柄变量    
InBlock.gif              strSelect += strTargetHandle;  //获取句柄    
InBlock.gif               //锁定并读取指定长度的数据    
InBlock.gif              strSelect +=  " readtext "  + strTableName +  "."  + strColumnName +  " @content @start @count HOLDLOCK" ;    
InBlock.gif              bytContent =  new   byte [intResidual];    
InBlock.gif              sqlcmd.Parameters[ "@start" ].Value = intCount * intBlock;    
InBlock.gif              sqlcmd.Parameters[ "@count" ].Value = intResidual;    
InBlock.gif              sqlcmd.CommandText = strSelect;    
InBlock.gif              bytContent = sqlcmd.ExecuteScalar()  as   byte [];    

InBlock.gif             fs.Write(bytContent,0,intResidual);    



本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/194146,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值