将文本文件解析为Datatable

时间:2018-04-16 15:22:14

标签: c# database parsing

我有一个遗留系统,可以按以下格式将订单信息导出到文本文件中,我需要将此数据导入到具有SQL数据库后端的MVC Web应用程序中。

我正在使用webclient从Web服务器获取文件,但现在我需要解析它以导入我的数据。

我需要字符串字段的双引号内的数据和数字的数字。

这是文件的样子:

SLOCATION   Pub   C  "FTL"  
MQUOTE      Pub   C  " "  
MCPAYDATE   Pub   C  "                    "  
MTAXNO      Pub   C  "                  "  
MCOUNTY     Pub   C  "   "  
MCUSTCAT    Pub   C  "RELIGIOUS ORGANIZATION"  
MRREF       Pub   N           4  (         4.00000000)  
MLREF       Pub   N           7  (         7.00000000)  
MLREC       Pub   N           4  (         4.00000000)  
MSUBTOT     Pub   N          67.4600  (        67.46000000)  
MCALL       Pub   C  " "  
PCATA       Pub   C  "X32-SER   "  
PCATB       Pub   C  "LD        "  
PCATC       Pub   C  "NP        "  
PCATD       Pub   C  "          "  
PCATE       Pub   L  .F.  
PCATF       Pub   L  .F.  
PCATG       Pub   L  .F.  
PCATH       Pub   L  .F.  
PCATI       Pub   L  .F.  
PCATJ       Pub   L  .F.  
PCATK       Pub   L  .F.  
PCATL       Pub   L  .F.  
PCATM       Pub   L  .F.  
PCATN       Pub   L  .F.  
PCATO       Pub   L  .F.  
PQUANA      Pub   N           1.00  (         1.00000000)  
PQUANB      Pub   N           1.00  (         1.00000000)  
PQUANC      Pub   N           1.00  (         1.00000000)  
PQUAND      Pub   N           0.00  (         0.00000000)  
PQUANE      Pub   L  .F.  
PQUANF      Pub   L  .F.  
PQUANG      Pub   L  .F.  
PQUANH      Pub   L  .F.  
PQUANI      Pub   L  .F.  
PQUANJ      Pub   L  .F.  
PQUANK      Pub   L  .F.  
PQUANL      Pub   L  .F.  
PQUANM      Pub   L  .F.  
PQUANN      Pub   L  .F.  
PQUANO      Pub   L  .F.  
PDONEA      Pub   C  "Test Award 6.5 in      
                                 "  
PDONEB      Pub   C  "LAST DONE                             
                                 "  
PDONEC      Pub   C  "John Doe                   
                                 "  
PDONED      Pub   C  "                                      
                                 "  
PDONEE      Pub   L  .F.  
PDONEF      Pub   L  .F.  
PDONEG      Pub   L  .F.  
PDONEH      Pub   L  .F.  
PDONEI      Pub   L  .F.  
PDONEJ      Pub   L  .F.  
PDONEK      Pub   L  .F.  
PDONEL      Pub   L  .F.  
PDONEM      Pub   L  .F.  
PDONEN      Pub   L  .F.  
PDONEO      Pub   L  .F.  
PDTWOA      Pub   C  "                                      
                                 "  
PDTWOB      Pub   C  "654321                                
                                 "  
PDTWOC      Pub   C  "                                      
                                 "  
PDTWOD      Pub   C  "                                      
                                 "  
PDTWOE      Pub   L  .F.  
PDTWOF      Pub   L  .F.  
PDTWOG      Pub   L  .F.  
PDTWOH      Pub   L  .F.  
PDTWOI      Pub   L  .F.  
PDTWOJ      Pub   L  .F.  
PDTWOK      Pub   L  .F.  
PDTWOL      Pub   L  .F.  
PDTWOM      Pub   L  .F.  
PDTWON      Pub   L  .F.  
PDTWOO      Pub   L  .F.  
PUNITA      Pub   N          67.46  (        67.46000000)  
PUNITB      Pub   N           0.00  (         0.00000000)  
PUNITC      Pub   N           0.00  (         0.00000000)  
PUNITD      Pub   N           0  (         0.00000000)  
PUNITE      Pub   N           0  (         0.00000000)  
PUNITF      Pub   N           0  (         0.00000000)  
PUNITG      Pub   N           0  (         0.00000000)  
PUNITH      Pub   N           0  (         0.00000000)  
PUNITI      Pub   N           0  (         0.00000000)  
PUNITJ      Pub   N           0  (         0.00000000)  
PUNITK      Pub   N           0  (         0.00000000)  
PUNITL      Pub   N           0  (         0.00000000)  
PUNITM      Pub   N           0  (         0.00000000)  
PUNITN      Pub   N           0  (         0.00000000)  
PUNITO      Pub   N           0  (         0.00000000)  
PTOTALA     Pub   N          67.4600  (        67.46000000)  
PTOTALB     Pub   N           0.0000  (         0.00000000)  
PTOTALC     Pub   N           0.0000  (         0.00000000)  
PTOTALD     Pub   N           0  (         0.00000000)  
PTOTALE     Pub   N           0  (         0.00000000)  
PTOTALF     Pub   N           0  (         0.00000000)  
PTOTALG     Pub   N           0  (         0.00000000)  
PTOTALH     Pub   N           0  (         0.00000000)  
PTOTALI     Pub   N           0  (         0.00000000)  
PTOTALJ     Pub   N           0  (         0.00000000)  
PTOTALK     Pub   N           0  (         0.00000000)  
PTOTALL     Pub   N           0  (         0.00000000)  
PTOTALM     Pub   N           0  (         0.00000000)  
PTOTALN     Pub   N           0  (         0.00000000)  
PTOTALO     Pub   N           0  (         0.00000000)  
XPTCK       Pub   C  "YES"  
MTUSER      Priv  C  "chris"                                  menumain.prg
SELECTNUM   Priv  N           1.00  (         1.00000000)     menumain.prg
SELECT      Priv  C  "1 "                                     menumain.prg
CONTINUE    Priv  C  "    "                                   newentry.prg
MDATE       Priv  C  "04/16/2018"                             newentry.prg
MPUDATE     Priv  C  "04/23/2018"                             newentry.prg
MPAYDATE    Priv  C  "  /  /    "                             newentry.prg
MGONEDATE   Priv  C  "  /  /    "                             newentry.prg
MSHPNAME    Priv  C  "ATTN: JOHN DOE"                         newentry.prg
MSHPCNME    Priv  C  "TEST COMPANY NAME             "         newentry.prg
MSHPAD1     Priv  C  "ANY STREET                    "         newentry.prg
MSHPAD2     Priv  C  "                              "         newentry.prg
MSHPCITY    Priv  C  "ANY CITY                      "         newentry.prg
MEMAIL      Priv  C  "test@email.com                        
                       "                                      newentry.prg
MPHOTO      Priv  C  "                              "         newentry.prg
MSHPST      Priv  C  "FL"                                     newentry.prg
MSTATE      Priv  C  "FL"                                     newentry.prg
MGONEPER    Priv  C  "  "                                     newentry.prg
MSHPZIP     Priv  C  "33312     "                             newentry.prg
MZIP        Priv  C  "33312     "                             newentry.prg
MSHIPVIA    Priv  C  "UPS GND"                                newentry.prg
MPUTIME     Priv  C  "1300"                                   newentry.prg
MGONETIME   Priv  C  "    "                                   newentry.prg
MCASH       Priv  C  " "                                      newentry.prg
MLETTER     Priv  C  " "                                      newentry.prg
MYTAX       Priv  C  " "                                      newentry.prg
MLABEL      Priv  C  "Y"                                      newentry.prg
MNUMBER     Priv  C  "TEST"                                   newentry.prg
MINVNO      Priv  C  "634835"                                 newentry.prg
MCUSTPO     Priv  C  "3123345             "                   newentry.prg
MSHIPFROM   Priv  C  "ACME Inc       "       newentry.prg
MCONTACT    Priv  C  "JOHN DOE              "                 newentry.prg
MPHONE      Priv  C  "954-111-2211          "                 newentry.prg
MFAX        Priv  C  "954-222-1122          "                 newentry.prg
MCELL       Priv  C  "                      "                 newentry.prg
MCOMMENT    Priv  C  "                                      
                                                    "         newentry.prg
MCOMMENT1   Priv  C  "                                      
                                                    "         newentry.prg
MACCTN      Priv  C  "TEST COMPANY NAME             "         newentry.prg
MSTREET     Priv  C  "ANY STREET                    "         newentry.prg
MCITY       Priv  C  "ANY CITY                      "         newentry.prg
MCHECKNO    Priv  C  "                              "         newentry.prg
MENGRAV     Priv  N           0  (         0.00000000)        newentry.prg
MTAX        Priv  N           4.0500000  (         4.05000000)  newentry.prg
MSHIPPING   Priv  N          15.57  (        15.57000000)     newentry.prg
MTOTAL      Priv  N            87.0800000  (        87.08000000)  newentry.prg
MPAYMENT    Priv  N           0  (         0.00000000)        newentry.prg
MCC         Priv  N           4  (         4.00000000)        newentry.prg
XSALESPER   Priv  C  "CN"                                     newentry.prg
MSNAME      Priv  C  "User  ext 510  "                        newentry.prg
MEVENTDTE   Priv  C  "04/23/2018"                             newentry.prg
MOPENACCT   Priv  C  "Y"                                      newentry.prg
MPAYPERSON  Priv  C  "TEST                          "         newentry.prg
ANS         Priv  C  "Y"                                      newentry.prg
ERASE       Priv  S                                           newentry.prg
MFILENAME   Priv  C  "634835"                                 dailwrte.prg

为了澄清,这里是我想通过解析这个数据来构建的模型模型,每个文件只包含一个实体的数据:

public class Order
    {
        [ScaffoldColumn(false)]
        public int Id { get; set; }

        public string SLOCATION { get; set; }
        public string MQUOTE { get; set; }
        public string MCPAYDATE { get; set; }
        public string MTAXNO { get; set; }
        public string MCOUNTY { get; set; }
        public string MCUSTCAT { get; set; }
        public int MRREF { get; set; }
        public int MLREF { get; set; }
        public int MLREC { get; set; }
        public decimal MSUBTOT { get; set; }
        public string MCALL { get; set; }
        public string PCATA { get; set; }
        public string PCATB { get; set; }
        public string PCATC { get; set; }
        public string PCATD { get; set; }
        public bool PCATE { get; set; }
        public bool PCATF { get; set; }
        public bool PCATG { get; set; }
        public bool PCATH { get; set; }
        public bool PCATI { get; set; }
        public bool PCATJ { get; set; }
        public bool PCATK { get; set; }
        public bool PCATL { get; set; }
        public bool PCATM { get; set; }
        public bool PCATN { get; set; }
        public bool PCATO { get; set; }
        public int PQUANA { get; set; }
        public int PQUANB { get; set; }
        public int PQUANC { get; set; }
        public int PQUAND { get; set; }
        public bool PQUANE { get; set; }
        public bool PQUANF { get; set; }
        public bool PQUANG { get; set; }
        public bool PQUANH { get; set; }
        public bool PQUANI { get; set; }
        public bool PQUANJ { get; set; }
        public bool PQUANK { get; set; }
        public bool PQUANL { get; set; }
        public bool PQUANM { get; set; }
        public bool PQUANN { get; set; }
        public bool PQUANO { get; set; }
        public string PDONEA { get; set; }
        public string PDONEB { get; set; }
        public string PDONEC { get; set; }
        public string PDONED { get; set; }
        public bool PDONEE { get; set; }
        public bool PDONEF { get; set; }
        public bool PDONEG { get; set; }
        public bool PDONEH { get; set; }
        public bool PDONEI { get; set; }
        public bool PDONEJ { get; set; }
        public bool PDONEK { get; set; }
        public bool PDONEL { get; set; }
        public bool PDONEM { get; set; }
        public bool PDONEN { get; set; }
        public bool PDONEO { get; set; }
        public string PDTWOA { get; set; }
        public string PDTWOB { get; set; }
        public string PDTWOC { get; set; }
        public string PDTWOD { get; set; }
        public bool PDTWOE { get; set; }
        public bool PDTWOF { get; set; }
        public bool PDTWOG { get; set; }
        public bool PDTWOH { get; set; }
        public bool PDTWOI { get; set; }
        public bool PDTWOJ { get; set; }
        public bool PDTWOK { get; set; }
        public bool PDTWOL { get; set; }
        public bool PDTWOM { get; set; }
        public bool PDTWON { get; set; }
        public bool PDTWOO { get; set; }
        public decimal PUNITA { get; set; }
        public int PUNITB { get; set; }
        public int PUNITC { get; set; }
        public int PUNITD { get; set; }
        public int PUNITE { get; set; }
        public int PUNITF { get; set; }
        public int PUNITG { get; set; }
        public int PUNITH { get; set; }
        public int PUNITI { get; set; }
        public int PUNITJ { get; set; }
        public int PUNITK { get; set; }
        public int PUNITL { get; set; }
        public int PUNITM { get; set; }
        public int PUNITN { get; set; }
        public int PUNITO { get; set; }
        public decimal PTOTALA { get; set; }
        public int PTOTALB { get; set; }
        public int PTOTALC { get; set; }
        public int PTOTALD { get; set; }
        public int PTOTALE { get; set; }
        public int PTOTALF { get; set; }
        public int PTOTALG { get; set; }
        public int PTOTALH { get; set; }
        public int PTOTALI { get; set; }
        public int PTOTALJ { get; set; }
        public int PTOTALK { get; set; }
        public int PTOTALL { get; set; }
        public int PTOTALM { get; set; }
        public int PTOTALN { get; set; }
        public int PTOTALO { get; set; }
        public string XPTCK { get; set; }
        public string MTUSER { get; set; }
        public int SELECTNUM { get; set; }
        public string SELECT { get; set; }
        public string CONTINUE { get; set; }
        public string MDATE { get; set; }
        public string MPUDATE { get; set; }
        public string MPAYDATE { get; set; }
        public string MGONEDATE { get; set; }
        public string MSHPNAME { get; set; }
        public string MSHPCNME { get; set; }
        public string MSHPAD1 { get; set; }
        public string MSHPAD2 { get; set; }
        public string MSHPCITY { get; set; }
        public string MEMAIL { get; set; }
        public string MPHOTO { get; set; }
        public string MSHPST { get; set; }
        public string MSTATE { get; set; }
        public string MGONEPER { get; set; }
        public string MSHPZIP { get; set; }
        public string MZIP { get; set; }
        public string MSHIPVIA { get; set; }
        public string MPUTIME { get; set; }
        public string MGONETIME { get; set; }
        public string MCASH { get; set; }
        public string MLETTER { get; set; }
        public string MYTAX { get; set; }
        public string MLABEL { get; set; }
        public string MNUMBER { get; set; }
        public string MINVNO { get; set; }
        public string MCUSTPO { get; set; }
        public string MSHIPFROM { get; set; }
        public string MCONTACT { get; set; }
        public string MPHONE { get; set; }
        public string MFAX { get; set; }
        public string MCELL { get; set; }
        public string MCOMMENT { get; set; }
        public string MCOMMENT1 { get; set; }
        public string MACCTN { get; set; }
        public string MSTREET { get; set; }
        public string MCITY { get; set; }
        public string MCHECKNO { get; set; }
        public int MENGRAV { get; set; }
        public decimal MTAX { get; set; }
        public decimal MSHIPPING { get; set; }
        public decimal MTOTAL { get; set; }
        public decimal MPAYMENT { get; set; }
        public string MCC { get; set; }
        public string XSALESPER { get; set; }
        public string MSNAME { get; set; }
        public string MEVENTDTE { get; set; }
        public string MOPENACCT { get; set; }
        public string MPAYPERSON { get; set; }
        public string ANS { get; set; }
        public string MFILENAME { get; set; }
    }

1 个答案:

答案 0 :(得分:-1)

请尝试以下代码:

Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "1003206");
dict.Add(2, "1234567");
dict.Add(3, "5432567");

int randomKey = RandomDictionaryKeyValue<int, string>(dict);