C#读取大文件(二)

System.Windows.Forms.Button btnOk =  null ;    
         System.Windows.Forms.Label lblShowPercent =  null ;    
          foreach (System.Windows.Forms.Control control  in  frmProgress.Controls)    
         {    
             if (control.GetType().ToString() ==  "System.Windows.Forms.ProgressBar" )    
            {    
             prgLoader = (System.Windows.Forms.ProgressBar)control;    
            }    
             if (control.GetType().ToString() ==  "System.Windows.Forms.Button" )    
            {    
             btnOk = (System.Windows.Forms.Button)control;    
            }    
             if (control.GetType().ToString() ==  "System.Windows.Forms.Label"  && control.Name ==  "lblShowPercent" )    
            {    
             lblShowPercent = (System.Windows.Forms.Label)control;    
            }    
         }    
             
         frmProgress.Show();    
          //启动转换    
         state = 2;    
 
          if (fs.Length > 2147483647 || fs.Length == 0)  //因为image列最多只能存储2,147,483,647个字节,所以这里做限定    
         {    
             return   false ;    
         }    
         intLength = ( int )fs.Length;    
         intCount = intLength / intBlock;    
 
          if (intCount == 0)    
         {    
            strSelect = strTargetInsert;    
            bytContent =  new   byte [intLength];    
            bytContent = br.ReadBytes(intLength);    
            sqlcmd.CommandText = strSelect;    
            sqlcmd.Parameters.Add( "@block" ,System.Data.SqlDbType.Image).Value = bytContent;    
            sqlcmd.ExecuteNonQuery();    
 
         }    
          else     
         {    
            strSelect = strTargetInsert;    
            bytContent =  new   byte [intBlock];    
            bytContent = br.ReadBytes(intBlock);    
 sqlcmd.CommandText = strSelect;    
            sqlcmd.Parameters.Add( "@block" ,System.Data.SqlDbType.Image).Value = bytContent;    
            sqlcmd.Parameters.Add( "@length" ,System.Data.SqlDbType.Int).Value = 0;    
            sqlcmd.ExecuteNonQuery();    
                            
             int  i = 1;    
             while (i != intCount)    
            {    
              if (state == 0)    
             {    
                strSelect =  "delete from "  + strTableName + strTargetHandle.Substring(strTargetHandle.LastIndexOf( " where " ));    
                sqlcmd.CommandText = strSelect;    
                sqlcmd.ExecuteNonQuery();    
                bytContent =  null ;    
                fs.Close();    
                 return   false ;    
             }    
             strSelect =  "declare @content varbinary(16) " ;    
             strSelect += strTargetHandle;    
             strSelect +=  " updatetext "  + strTableName +  "."  + strColumnName +  " @content @length 0 @block" ;    
                 
             bytContent = br.ReadBytes(intBlock);    
 
             sqlcmd.Parameters[ "@block" ].Value = bytContent;    
             sqlcmd.Parameters[ "@length" ].Value = i * intBlock;    
             sqlcmd.CommandText = strSelect;    
             sqlcmd.ExecuteNonQuery();    
                 
             intPercent = ( int )((( double )(i * intBlock)) / (( double )intLength) * 100);    
             prgLoader.Value = intPercent;    
             lblShowPercent.Text = prgLoader.Value.ToString() +  "%" ;    
             ++i;    
             Application.DoEvents();    
            }    
 
             int  intResidual = intLength % intBlock;    
     if (intResidual > 0)    
            {    
             strSelect =  "declare @content varbinary(16) " ;    
             strSelect += strTargetHandle;    
             strSelect +=  " updatetext "  + strTableName +  "."  + strColumnName +  " @content @length 0 @block" ;    
 
             bytContent =  new   byte [intResidual];    
             bytContent = br.ReadBytes(intResidual);    
                                            
             sqlcmd.Parameters[ "@block" ].Value = bytContent;    
             sqlcmd.Parameters[ "@length" ].Value = intCount * intBlock;    
             sqlcmd.CommandText = strSelect;    
             sqlcmd.ExecuteNonQuery();            
            }    
         }    
         prgLoader.Value = 100;    
         lblShowPercent.Text = prgLoader.Value.ToString() +  "%" ;    
         btnOk.Text =  "关闭" ;    
 
        }    
         else     
        {    
          if (fs.Length > 2147483647 || fs.Length == 0)  //因为image列最多只能存储2,147,483,647个字节,所以这里做限定    
         {    
             return   false ;    
         }    
         intLength = ( int )fs.Length;    
         intCount = intLength / intBlock;    
 
          if (intCount == 0)    
         {    
            strSelect = strTargetInsert;    
            bytContent =  new   byte [intLength];    
            bytContent = br.ReadBytes(intLength);    
            sqlcmd.CommandText = strSelect;    
            sqlcmd.Parameters.Add( "@block" ,System.Data.SqlDbType.Image).Value = bytContent;    
            sqlcmd.ExecuteNonQuery();    
         }    
          else     
         {    
            strSelect = strTargetInsert;    
bytContent =  new   byte [intBlock];    
            bytContent = br.ReadBytes(intBlock);    
            sqlcmd.CommandText = strSelect;    
            sqlcmd.Parameters.Add( "@block" ,System.Data.SqlDbType.Image).Value = bytContent;    
            sqlcmd.Parameters.Add( "@length" ,System.Data.SqlDbType.Int).Value = 0;    
            sqlcmd.ExecuteNonQuery();    
            
             int  i = 1;    
             while (i != intCount)    
            {    
             strSelect =  "declare @content varbinary(16) " ;    
             strSelect += strTargetHandle;    
             strSelect +=  " updatetext "  + strTableName +  "."  + strColumnName +  " @content @length 0 @block" ;    
                 
             bytContent = br.ReadBytes(intBlock);    
 
             sqlcmd.Parameters[ "@block" ].Value = bytContent;    
             sqlcmd.Parameters[ "@length" ].Value = i * intBlock;    
             sqlcmd.CommandText = strSelect;    
             sqlcmd.ExecuteNonQuery();    
             ++i;    
            }    
 
             int  intResidual = intLength % intBlock;    
             if (intResidual > 0)    
            {    
             strSelect =  "declare @content varbinary(16) " ;    
             strSelect += strTargetHandle;    
             strSelect +=  " updatetext "  + strTableName +  "."  + strColumnName +  " @content @length 0 @block" ;    
 
             bytContent =  new   byte [intResidual];    
             bytContent = br.ReadBytes(intResidual);    
                                            
             sqlcmd.Parameters[ "@block" ].Value = bytContent;    

             sqlcmd.Parameters["@length"].Value = intCount * intBlock;    



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

你可能感兴趣的:(C#读取大文件(二))