【问题标题】:How to Store txt information in database SQL Server如何在数据库 SQL Server 中存储 txt 信息
【发布时间】:2011-03-06 23:51:06
【问题描述】:

使用 VS08 C# 窗口。我有 txt 文件 。读取文件后,我需要将文件信息存储在数据库中。我的SQL Server数据库表结构如下:

CREATE TABLE [dbo].[StoreTxtValues]
(
  [PortCode] [int] (80) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
  1)[Vessel] [varchar] (800) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
  2)[Voyage] [varchar] (800) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
  3)[Sailed] [datetime] NOT NULL,
  4)[Carrier] [varchar] (800) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,  
  5)[LoadingContainer] [varchar] (800) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,  
  6)[DischargeSeal] [varchar] (800) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
  7)[rowValue] [varchar] (8000) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

)

请查看图片中的这些数字。我需要帮助来存储我描述的数据。任何建议都会被接受。提前致谢。

【问题讨论】:

    标签: c# .net text windows


    【解决方案1】:

    逐行解析文件以获取您需要的信息。使用System.IO.FileStream或StreamReader加载数据,然后根据需要进行处理。将其保存到数据库中。

    下面是使用来自here 的StreamReader 的快速示例。

    using (StreamReader sr = new StreamReader(path))   
    {  
        while (sr.Peek() >= 0)   
        {  
            string lineFromFile = sr.ReadLine();  
            //now parse the string lineFromFile and get the information you need  
        }  
    } 
    

    我可能会补充一点,创建对象来表示您需要存储的各种数据可能会有所帮助。然后,该对象可以更轻松地处理数据,将其保存到数据库等。

    解析字符串(假设文件都是完全相同的格式,即相同的信息行)。

    跟踪您所在的线路。

    string[] myFile = File.ReadAllLines(@"C:\file.txt");
    
    var lineCount = myFile.Length;
    
         for(i=0; i<lineCount; i++)  
         {
              switch(i)
              {
                   case 0:
                       //parse line 0
                       break;
                   case 1:
                       //parse line 1
                       break;
                   case 2:
                       //parse line 2
                       break;
                   //and so on until you get to the line that begins the regular "records" part of the file
              }
    
              if(i >= 10) //assumes line 10 is where the regular "records" start
              {
                  //parse the record like so
                  string[] columns = myFile[i].Split('\t'); //this assumes your columns are separated by a tab. If it's a space you would use myFile[i].Split(' '), etc. 
                  //now you have an array with all your columns
                  foreach(string column in columns)
                  {
                       //now do what you want with the information
    
                  }
              }
         }
    }
    

    【讨论】:

    • StreamReader sStreamReader = new StreamReader(FilePath);string AllData = sStreamReader.ReadToEnd();现在我得到了 AllData 中的所有信息,而不是做什么。如上所述如何解析数据。使用 linq 有用吗,请您显示一些语法
    • 您可能希望一次阅读一行,正如我刚刚在我的回答中的编辑中指出的那样。这有帮助吗?
    • 我知道如何读取 txt 文件。我需要帮助如何解析存储在字符串中的读取数据。如果有任何疑问请询问
    • 请看图片。读取文件后,我需要在表格中存储 1),2),3),4),5),6),7) 数字信息。我的问题是如何我可以分别为每列选择每个数据吗?
    • 感谢您的回复。尤其是您的语法。有一些问题,假设数字 1) 和 2) 有额外的空间,现在如果我取行号,那么我会错过信息。另一个主要问题是我的输入文本文件有多个输入信息,我在图片。它们是个体,每个都是相同的甲酸盐。比如何克服这个问题
    【解决方案2】:

    按照理查兹的回答,

    在我看来,您需要多个表。

    表 1,可能称为 ManifestHeader,包含第 1-6 列。

    表 2,可能称为 ManifestDetails 并包含第 7 列(可以细分为更多字段,例如 LoadingContainer、DischargeSeal、Dest 等)。

    您的数据是固定宽度和逗号分隔(第 6 列)的混合体,您可以使用 String.Substring 和 String.Split 来分隔行列。

    例如

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Collections;
    
    public class MyClass
    {
        public static void ProcessFile()
        {
            TextReader textReader = new StreamReader(@"C:\DEV\DATA\MyDataFile.txt");
            string s = textReader.ReadLine();  
            string col1 = s.Substring(9, 29).Trim();  
    
            s = textReader.ReadLine();  
            string col2 = s.Substring(9, 29).Trim();  
    
            s = textReader.ReadLine();  
            string col3 = s.Substring(9, 29).Trim();  
    
            s = textReader.ReadLine();  
            string col4 = s.Substring(9, 29).Trim();
    
    
            s = textReader.ReadLine();
            s = textReader.ReadLine();
            s = textReader.ReadLine();
            s = textReader.ReadLine();
            s = textReader.ReadLine();
            s = textReader.ReadLine();
    
            s = textReader.ReadLine();  
            string col5 = s.Split(",")[0].Trim();
            string col6 = s.Split(",")[1].Trim();
    
            while ((s = textReader.ReadLine()).Trim() != "")
            {
                string LoadingContainer = s.Substring(0, 10).Trim();  
                string DischargeSeal = s.Substring(13, 23).Trim();  
                string Dest  = s.Substring(25, 25).Trim();
                // Parse further colums here...
                // Insert record into the database
            }
        }
    
        public static void Main()
        {
            try
            {
                ProcessFile();
            }
            catch (Exception e)
            {
                string error = string.Format("---\nThe following error occurred while executing the program:\n{0}\n---", e.ToString());
                Console.WriteLine(error);
            }
            finally
            {
                Console.Write("Press any key to continue...");
                Console.ReadKey();
            }
        }
    
    }
    

    【讨论】:

    • 我的txt文件格式是固定的,但是数据不固定,比如何设置索引号
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2013-07-09
    • 2012-10-12
    • 2015-11-10
    • 2021-07-31
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多