如何在定义的格式的文本文件中写入数据库值

时间:2010-07-20 11:57:27

标签: asp.net reporting asp.net-3.5

我将分隔的文本文件读入数据集。我正在另一个文本文件中逐行编写这些数据集值。我的C#语法是:

try
            {
                DataClasses1DataContext db = new DataClasses1DataContext();

                var r = from p in db.Products
                        select p;
                //DataTable TarGetTable;
                //TarGetTable = AddDataToTable();
                String TargetStringFilePath;
                TargetStringFilePath = @"c:\aaa.txt";
                StreamWriter fw = File.CreateText(TargetStringFilePath);

                foreach (Product drPrintRow in r)
                {

                    //drPrintRow[drPrintRow]
                    String str = drPrintRow.CategoryID.ToString();
                    str +=" "+ drPrintRow.Discontinued.ToString();
                    str +=" "+ drPrintRow.ProductID.ToString();
                    str += " "+drPrintRow.ProductName.sToString();
                    fw.WriteLine(str);
                }


                fw.Flush();

                fw.Close();
                //}
            }
            catch (Exception ex)
            {
            }

我希望此输出采用以下格式

                                        CUSTOMERWISE ORDER STATUS REPORT
                                        ================================
 Trading Date : 1-2-2010                                                                           Date :1-2-2010
                                                                                                     Time :15:00:52                    
      HOWLA TYPE               FILL TYPE              MKT TYPE                   ORDER STATUS                     
    --------------     -------------------------     -----------     ---------------------------------------------
    D : DVP            MF   : MinimumFill            P : PUBLIC      AC : ACTIVE       CF : CompletelyFilled      
    N : NORMAL         PF   : Partial Fill           S : SPOT        NA : NOTACTIVE    FR : FilledAndRejected     
    U : UNSPECIFIED    FOK  : FillOrKill             O : ODDLOT      XE : Executed     EX : Expired               
                       AON  : AllOrNone              B : BLOCK       RJ : Rejected     OM : OriginalOrderModified 
                       PFAK : PartialFillAndKill                     DE : Deactivated  PF : PartiallyFilled       
                                                                     DL : Deleted      CR : CrossingReported      
_______________________________________________________________________________________________________________________
CUSTOMER  |INSTRUMENT| BROKER  | Quantity|B |   PRICE  | BALANCE |HOWLA| EXPIRY   | MKT|FILL|ORDER |  TORN   |  TORN   |
          |          | ORDER   |         |/ |    Tk.   |   QTY   |TYPE | DATE     |TYPE|TYPE|STATUS| PREFIX  | SUFFIX  |
          |          | REFNO   |         |S |          |         |     |          |    |    |      |   NO    |   NO    |
__________|__________|_________|_________|__|__________|_________|_____|__________|____|____|______|_________|_________|
00002     |BSRMSTEEL |109379297|      500|S |   1325.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 41500123|
          |CITYBANK  |109379298|      200|S |    940.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  6300609|
          |POWERGRID |109379303|      500|S |    789.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  6000269|
          |SPCERAMICS|109379306|     5000|S |     93.60|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 40200627|
          |CITYBANK  |109379439|     2000|S |    912.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  6300918|
00004     |PREMIERBAN|109379645|      550|B |    402.75|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 32601990|
          |PLFSL     |109379725|      100|B |    660.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 27800430|
          |MAKSONSPIN|109379373|     2000|S |    110.70|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 41400304|
          |MPETROLEUM|109379654|      100|S |    329.00|      100|  N  |01-02-2010| P  | PF |  AC  |201003201| 36001036|
          |BXPHARMA  |109379867|      200|S |    185.50|      200|  N  |01-02-2010| P  | PF |  AC  |201003201|  9302082|
00011     |PADMAOIL  |109379692|     1000|S |    811.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 15500272|
          |MPETROLEUM|109379989|     1000|S |    324.70|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 36001454|
          |CONFIDCEM |109380341|      500|S |   1614.75|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  6500878|
          |          |109380346|      500|S |   1616.00|      240|  N  |01-02-2010| P  | PF |  PF  |201003201|  6500891|
          |          |109380351|      500|S |   1615.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  6500905|
00012     |APEXTANRY |109379064|      100|B |   1373.50|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  1300042|
          |          |109379154|      100|B |   1365.00|      100|  N  |01-02-2010| P  | PF |  AC  |201003201|  1300127|
          |          |109379328|      100|B |   1385.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  1300301|
          |UNIONCAP  |109379471|      500|B |    143.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  8000591|
          |          |109379535|      500|B |    143.50|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  8000651|
          |          |109379774|     1000|B |    142.30|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  8000931|
          |          |109380425|      500|B |    142.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  8001532|
          |PREMIERBAN|109379218|      100|S |    399.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 32600814|
          |SUMITPOWER|109380383|      100|S |   1324.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 28401076|
          |          |109380438|       50|S |   1321.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201| 28401129|
00013     |APEXTANRY |109379034|      100|B |   1375.00|        0|  N  |01-02-2010| P  | PF |  CF  |201003201|  1300016|

如何以指定格式在文本文件中写入数据集值。是否有任何现有软件可以为我做这些。

我在C#syntax.HOw上使用Northwind数据库以上述格式编写此产品表信息。

2 个答案:

答案 0 :(得分:0)

我建议从string.Format方法开始,并检查documentation page上的其他参考资料,以获取有关确定正确的自定义格式字符串的帮助。

答案 1 :(得分:0)

我认为您正在寻找string.Format(...)。这允许您将许多不同的数据类型格式化为定义的字符串格式。