C#读取大文件(一)

简介:
最近一个项目需要将大文件写入和读取到数据库,觉得可能很多人也需要相关得东西,所以就将代码帖出来    
InBlock.gif  
InBlock.gif protected   int  state = 0;  //表示进度条当前处理的事件类型,1表读取word,2表写入word,3表doc转pdf,4表txt转pdf    
InBlock.gif  
InBlock.gif private  System.Windows.Forms.Form getDialog( string  strFormName,System.Drawing.Icon ico, string  strShowContent)    
InBlock.gif     {    
InBlock.gif      System.Windows.Forms.Form frm =  new  Form();    
InBlock.gif       //初始化窗体    
InBlock.gif      frm.Text = strFormName;    
InBlock.gif      frm.Icon = ico;    
InBlock.gif      frm.MaximizeBox =  false ;    
InBlock.gif      frm.MinimizeBox =  false ;    
InBlock.gif      frm.TopMost =  true ;    
InBlock.gif      frm.ShowInTaskbar =  false ;    
InBlock.gif      frm.Height = 168;    
InBlock.gif      frm.Width = 544;    
InBlock.gif      frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;    
InBlock.gif  
InBlock.gif       //添加控件    
InBlock.gif      System.Windows.Forms.Label lblContent =  new  Label();    
InBlock.gif      lblContent.Text = strShowContent;    
InBlock.gif                         lblContent.Left = 30;    
InBlock.gif      lblContent.Top = 20;    
InBlock.gif      lblContent.Text = strShowContent;    
InBlock.gif      frm.Controls.Add(lblContent);    
InBlock.gif                             
InBlock.gif                         System.Windows.Forms.ProgressBar prgLoader =  new  ProgressBar();    
InBlock.gif      prgLoader.Left=30;    
InBlock.gif      prgLoader.Top = lblContent.Top + lblContent.Height + 5;    
InBlock.gif      prgLoader.Width = frm.Width - 2 * 30;    
InBlock.gif      frm.Controls.Add(prgLoader);    
InBlock.gif          
InBlock.gif      System.Windows.Forms.Label lblShowPercent =  new  Label();    
InBlock.gif      lblShowPercent.TextAlign = System.Drawing.ContentAlignment.MiddleRight;    
InBlock.gif      lblShowPercent.Left = prgLoader.Width + 30 - lblShowPercent.Width;    
InBlock.gif      lblShowPercent.Top = prgLoader.Height + prgLoader.Top + 5;    
InBlock.gif                         lblShowPercent.Text = prgLoader.Value.ToString() +  "%" ;    
InBlock.gif      lblShowPercent.Name =  "lblShowPercent" ;    
InBlock.gif      frm.Controls.Add(lblShowPercent);    
InBlock.gif  
InBlock.gif      System.Windows.Forms.Button btnOK =  new  Button();    
InBlock.gif      btnOK.Text =  "取消" ;    
InBlock.gif      btnOK.Left = prgLoader.Width + 30 - btnOK.Width;    
InBlock.gif btnOK.Top = frm.Height - 30 - btnOK.Height;    
InBlock.gif      btnOK.Click += new  EventHandler(btnOk_Click);    
InBlock.gif      frm.Controls.Add(btnOK);    
InBlock.gif                             
InBlock.gif       return  frm;    
InBlock.gif     }    
InBlock.gif  
InBlock.gif      private   void  btnOk_Click( object  sender,System.EventArgs e)    
InBlock.gif     {    
InBlock.gif           //获取控件信息    
InBlock.gif      System.Windows.Forms.Button btnOk = (System.Windows.Forms.Button)sender;    
InBlock.gif          System.Windows.Forms.Form frm = (System.Windows.Forms.Form)btnOk.Parent;    
InBlock.gif      System.Windows.Forms.ProgressBar prgLoader =  null ;    
InBlock.gif       foreach (System.Windows.Forms.Control control  in  frm.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      }    
InBlock.gif       //判断当前的完成情况    
InBlock.gif       if (prgLoader.Value == 100)    
InBlock.gif      {    
InBlock.gif         frm.Close();    
InBlock.gif      }    
InBlock.gif       else     
InBlock.gif      {    
InBlock.gif         System.Windows.Forms.DialogResult dr = MessageBox.Show(frm, "是否停止当前操作?" , "提示" ,System.Windows.Forms.MessageBoxButtons.YesNo,    
InBlock.gif          System.Windows.Forms.MessageBoxIcon.Warning);    
InBlock.gif          if (dr == System.Windows.Forms.DialogResult.Yes)    
InBlock.gif         {     
InBlock.gif          state = 0;    
InBlock.gif          frm.Close();            
InBlock.gif         }    
InBlock.gif  
InBlock.gif      }    
InBlock.gif     }    
InBlock.gif  
/// <summary>    
InBlock.gif                  /// 写入word到数据库    
InBlock.gif                  /// </summary>    
InBlock.gif                  /// <param name="sqlcon">sql连接类</param>    
InBlock.gif                  /// <param name="strTargetInsert">直接将目标内容写入数据库的sql,"insert into 表名(目标列名) values (@block)"</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  WriteWordDocument(System.Data.SqlClient.SqlConnection sqlcon, string  strTargetInsert, 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;  //获取文件内容的长度    
InBlock.gif       string  strSelect = "";  //要执行的sql查询语句    
InBlock.gif       byte  []bytContent =  null //定义内容数组    
InBlock.gif       //比例    
InBlock.gif              int  intPercent = 0;    
InBlock.gif       //建立要输入的文件流    
InBlock.gif                         System.IO.FileStream fs =  null ;    
InBlock.gif       //建立二进制读取    
InBlock.gif      System.IO.BinaryReader br =  null ;    
InBlock.gif       try     
InBlock.gif      {    
InBlock.gif         fs =  new  FileStream(strPath,System.IO.FileMode.Open);    
InBlock.gif      }    
InBlock.gif       catch     
InBlock.gif      {    
InBlock.gif          return   false ;    
InBlock.gif      }    
InBlock.gif      br =  new  BinaryReader((Stream)fs);    
InBlock.gif                          //为关键参数赋值    
InBlock.gif       try     
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;    



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

相关文章
|
3月前
|
Go
文件写入、追加、读取、复制不是问题
文件写入、追加、读取、复制不是问题
23 0
|
4月前
|
C语言
从文件中读取一行
C 语言实例 - 从文件中读取一行。
34 3
|
4月前
|
C#
C# (File方法)对文件的操作,字节写入和读取
C# (File方法)对文件的操作,字节写入和读取
|
4月前
|
XML C# 数据格式
C#读取写入文件的三种方式
最近对文件的操作比较频繁。这里记录一下常用的几种文件读写的方式。 我这里使用窗体来做测试,例子在文末,可下载。
50 0
|
9月前
从文件1中读取数据,修改数据的内容,然后存入文件2中,再从文件2中把数据读取出来显示在屏幕上
从文件1中读取数据,修改数据的内容,然后存入文件2中,再从文件2中把数据读取出来显示在屏幕上
109 0
|
11月前
|
计算机视觉 索引 Windows
视频操作_01视频读写:视频读写+读取视频+保存视频
在OpenCV中我们要获取一个视频,需要创建一个VideoCapture对象
137 0
|
开发者 Python Windows
文件的读取方式 | 学习笔记
快速学习 文件的读取方式
75 0
文件的读取方式 | 学习笔记
C#编程-35:写入读取文本文件
C#编程-35:写入读取文本文件
|
大数据 C#
C# 读取大文件 (可以读取3GB大小的txt文件)
原文:C# 读取大文件 (可以读取3GB大小的txt文件) 在处理大数据时,有可能 会碰到 超过3GB大小的文件,如果通过 记事本 或 NotePad++去打开它,会报错,读不到任何文件。
4111 1