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

发布于 2024-09-11 02:36:56 字数 6369 浏览 5 评论 0原文

我将分隔文本文件读入数据集中。我正在另一个文本文件中逐行写入这些数据集值。我的 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#语法上的Northwind数据库。如何按上述格式编写此产品表信息。

I read a delimited text file into dataset. i am writing these dataset values line by line in another text file. my C# syntax is :

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)
            {
            }

I want this output in bellow format

                                        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|

How to write dataset value in text file in specified format.Is there any existing software that can do that for me.

I use Northwind database on C# syntax.HOw to write this product table information in above format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多孤肩上扛 2024-09-18 02:36:56

我建议从 字符串开始。 Format 方法并检查 文档页面帮助确定正确的自定义格式字符串。

I'd suggest starting with the string.Format method and check the additional references on the documentation page for help in determining the proper custom format strings.

孤檠 2024-09-18 02:36:56

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

I think you are looking for string.Format(...). This allows you to format many different data types into a defined string format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文