將資料庫中位元據以非同步方式寫入磁碟

來源:互聯網
上載者:User

方式一:一次擷取,非同步寫入
/// <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);
    //擷取檔案資料
    ImageContent = new byte[Convert.ToInt64(reader.GetString(7))];
    reader.GetBytes(6,0,ImageContent,0,Convert.ToInt32(reader.GetString(7))); 
    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();
   }
   reader.Close();
   //關閉串連
   if(Cnn.State == System.Data.ConnectionState.Open)
   {
    Cnn.Close();
   } 
   strErr = "";
   //釋放資源
   Cnn.Dispose();
   cmd.Dispose();
   GC.Collect();
   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();
   }
  }

 兩種方式的比較:
 
方式一:適合於資料庫負載較大,位元據大小已知的情況;
方式二:適合於資料庫負載較小,位元據大小未知的情況;

其中:兩種方式的非同步機制都是相同的,沒有任何區別;非同步機制的優點在於充分發揮了作業系統的優點
注意:在需要對效能進行同比測試的上下文中不能採用非同步機制而必須盡量採用同步機制,以提高真實性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.