2012-07-31 49 views
0

我正在使用c#編寫創建一個文本文件。我正在嘗試在文本文件的一行中寫入2條記錄。我的數據源來自存儲過程的結果集。C#將多條記錄寫入一行

這是我的代碼。目前我只是通過唱片錄製唱片。我想在一行中寫入2條記錄到文本文件中。

例如:

記錄1條記錄6

記錄2記錄7

記錄3記錄8

記錄4記錄9

記錄5記錄10

創下11條記錄16

記錄12條17

記錄13記錄18

記錄14記錄19

記錄15記錄20

public bool SalesPriceIndexProcess() 
    { 
     int page = 1, count = 1; 
     FileStream fs = new FileStream(Properties.Settings.Default.Path + SalesPriceDirectoryReportName, FileMode.Create); 
     StreamWriter sw = new StreamWriter(fs); 
     foreach (ManhattanLUSESalesPriceIndex_Result s in _db.ManhattanLUSESalesPriceIndex()) 
     { 
      if (count == 1) 
      { 
       SalesPriceHeaderWrite(s.BldgClassCd, ref fs, ref sw); 
      } 
      string saleDate = s.SaleDt.ToString(); 
      sw.WriteLine("{0,13:C0}{1,-5} {2,-4} {3,5} {4,3} {5,15:C0}{6} {7,-8} {8,4}" 
       , s.SalePriceAmt, s.MultiSplitCd 
       , Convert.ToInt16(s.StoriesNbr) + s.LandUseMajorCd 
       , (s.LegalBlkId + "-"), s.LegalLot 
       , s.TransAssdTotalAmt, s.BldgClassCd.PadRight(2) 
       , (saleDate.Length > 6 ? saleDate.Substring(4, 2) + "/" + saleDate.Substring(6, 2) + "/" + saleDate.Substring(2, 2) : "") 
       , page  
       ); 

      PclIdPageIndexDict.Add(s.PclId, page); 
      if (count % 5 == 0) 
      { 
       sw.WriteLine(SalesPriceLineBreak); 
      } 
      if (count % 145 == 0) 
      { 
       sw.WriteLine(SalesPricePageBreak); 
      } 

      count++; //increment by 1 
      page = count % 145; 
     } 
     return true; 
+0

請告訴我問題嗎? – 2012-07-31 15:42:22

+0

如何將數據集中的2條記錄寫入同一行到文本文件中? – user1566191 2012-07-31 15:51:42

回答

0

你將需要存儲的最後5行在隊列中。當你寫出你行,你就需要這樣說:

while(!eof) { 
    for (int i=0; i<5; i++) { 
    queue.enqueue(readline()); 
    } 

    for (int i=0; i<5; i++) { 
    write(queue.dequeue()); 
    write(readline()); 
    write('\n'); 
    } 
} 

您對C#語法或做線對邏輯問題?

+0

實際上。我對c#很新,所以對語法的解釋很有幫助。 – user1566191 2012-07-31 16:21:09

0
if(_db.ManhattanLUSESalesPriceIndex().Count() < 5) 
{ 
// throw 
} 
else 
{ 
    var collectionStore = _db.ManhattanLUSESalesPriceIndex().ToList(); 
    for(int i = 5; i < collectionStore.Count(); i++) 
    { 
     var recordN = collectionStore[i-5]; 
     // do whatever outputting you want 
     var recordNPlusFive = collectionStore[i]; 
     // do whatever outputting you want 
     } 
    } 
} 

編輯的代碼

+0

使用Get給我一個錯誤。我是否缺少參考? – user1566191 2012-07-31 17:03:21

+0

對不起,我沒有檢查我的語法,寫了它,讓我的語言錯了。更新我的帖子來幫助你。 – user989056 2012-07-31 21:05:51