日期:2012-05-22  浏览次数:20430 次

方式一:一次获取,异步写入
/// <summary>
/// 缓冲区大小
/// </summary>
public const int numPixels = 512 * 512;
/// <summary>
/// 将数据文件写入磁盘
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static bool MakeFileWithWriteListByAdapter(string strSql,out string strErr)
{
if(File.Exists(ConfigProxy.GetValueByKey("ListFile")))File.Delete(ConfigProxy.GetValueByKey("ListFile"));
DataTable objTable;
if(!OleDataBaseProxy.ExecuteSql(strSql,out objTable,out strErr))return false;
string outputPath = ConfigProxy.GetValueByKey("OutputPath");
if(objTable.Rows.Count < 1) return false;
string strDirectory = outputPath + "\\";
if(!Directory.Exists(strDirectory)) Directory.CreateDirectory(strDirectory);
for(int i = 0;i< objTable.Rows.Count; i ++)
{

string fileName = objTable.Rows[i]["附件名称"].ToString();
//记录输出列表
LogProxy.WriteList(strDirectory + fileName);
//获取文件数据
byte [] ImageContent = (byte[])objTable.Rows[i]["附件内容"];
AutoResetEvent manualEvent = new AutoResetEvent(false);
FileStream fStream =
new FileStream(strDirectory + fileName,FileMode.Create,
FileAccess.ReadWrite, FileShare.None, 4096, true);
IAsyncResult asyncResult = fStream.BeginWrite(
ImageContent, 0, ImageContent.Length,
new AsyncCallback(EndWriteCallback),
new State(fStream, manualEvent));
manualEvent.WaitOne(5000, false);
fStream.Close();
}
strErr = "";
return true;
}
class State
{
public FileStream fStream;
public AutoResetEvent autoEvent;

public State(FileStream fStream, AutoResetEvent autoEvent)
{
this.fStream = fStream;
this.autoEvent = autoEvent;
}
}
static void EndWriteCallback(IAsyncResult asyncResult)
{

State stateInfo = (State)asyncResult.AsyncState;
int workerThreads;
int portThreads;
try
{
ThreadPool.GetAvailableThreads(out workerThreads,
out portThreads);
stateInfo.fStream.EndWrite(asyncResult);
Thread.Sleep(1500);
}
finally
{
stateInfo.autoEvent.Set();
}
}

方式二:联机读取,异步写入

/// <summary>
/// 缓冲区大小
/// </summary>
public const int numPixels = 512 * 512;
/// <summary>
/// 将数据文件写入磁盘
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static bool MakeFileWithWriteListByReader(string strSql,out string strErr)
{
if(File.Exists(ConfigProxy.GetValueByKey("ListFile")))File.Delete(ConfigProxy.GetValueByKey("ListFile"));
string outputPath = ConfigProxy.GetValueByKey("OutputPath");
string strDirectory = outputPath + "\\";
if(!Directory.Exists(strDirectory)) Directory.CreateDirectory(strDirectory);
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand();
OleDbConnection Cnn = new OleDbConnection(ConfigProxy.GetValueByKey("OleConnectionString"));
cmd.Connection = Cnn;
cmd.CommandText = strSql;
//开启连接
try
{
Cnn.Open();
}
catch(Exception Err)
{
strErr = Err.Message;
return false;
}
byte[] pixels = new byte[numPixels];
OleDbDataReader reader = cmd.ExecuteReader();
byte[]ImageContent;
//逐条处理
while(reader.Read())
{
string fileName = reader.GetString(1);
//记录输出列表
LogProxy.WriteList(strDirectory + fileName);