C#读取大文件(四)

    }    
            prgLoader.Value = 100;    
            lblShowPercent.Text = prgLoader.Value.ToString() + "%";    
            btnOk.Text = "关闭";    
         }    
        }    
         else    
        {    
         if(intCount == 0)    
         {    
            strSelect = strTargetSelect;    
            sqlcmd.CommandText = strSelect;    
            bytContent = new byte[intLength];    
            bytContent = sqlcmd.ExecuteScalar() as byte[];    
            fs.Write(bytContent,0,intLength);    
         }    
         else    
         {    
             int i = 0;    
            bytContent = new byte[intBlock];    
             while(i < intCount)    
            {    
             strSelect = "declare @content varbinary(16) "; //再sql中声明获取目标image列内容的句柄变量    
             strSelect += strTargetHandle; //获取句柄    
             //锁定并读取指定长度的数据    
             strSelect += " readtext " + strTableName + "." + strColumnName + " @content @start @count HOLDLOCK";    
             if(i == 0)    
             {    
                 //添加@start和@count变量,分别表偏移变量和取的长度    
                sqlcmd.Parameters.Add( "@start",System.Data.SqlDbType.Int).Value = 0;    
                sqlcmd.Parameters.Add( "@count",System.Data.SqlDbType.Int).Value = intBlock;    
             }    
             else    
             {    
                sqlcmd.Parameters[ "@start"].Value = i * intBlock;    
                sqlcmd.Parameters[ "@count"].Value = intBlock;    
             }    
             sqlcmd.CommandText = strSelect;    
             bytContent = sqlcmd.ExecuteScalar() as byte[];    
fs.Write(bytContent,0,intBlock);    
             ++i;    

            }    
             //将剩余的字节写入流    
             int intResidual = intLength % intBlock;    
             if(intResidual > 0)    
            {    
             strSelect = "declare @content varbinary(16) "; //再sql中声明获取目标image列内容的句柄变量    
             strSelect += strTargetHandle; //获取句柄    
             //锁定并读取指定长度的数据    
             strSelect += " readtext " + strTableName + "." + strColumnName + " @content @start @count HOLDLOCK";    
             bytContent = new byte[intResidual];    
             sqlcmd.Parameters[ "@start"].Value = intCount * intBlock;    
             sqlcmd.Parameters[ "@count"].Value = intResidual;    
             sqlcmd.CommandText = strSelect;    
             bytContent = sqlcmd.ExecuteScalar() as byte[];    
             fs.Write(bytContent,0,intResidual);    

            }    
         }    

        }    
         bytContent = null;    
         fs.Close();    
     }    
     catch    
     {    
            state = 0;    
            System.IO.File.Delete(strPath);    
                                    bytContent = null;    
                                    fs.Close();    
             return false;    
     }    
     state = 0;    
     return true;    
    }

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