Work on VS08 C# window. I have txt file . After read the file, I need to store the file information in database. My SQL Server database table structure is below:
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,
)
Please see those numbers in the picture. I need help to store the data as I describe on it. Any suggestion will be accepted. Thanks in advance.
Parse the file line by line to get the information you need. Use
System.IO.FileStreamorStreamReaderto load the data, and then you can process it as needed. Save it to the database.Here’s a quick example for using
StreamReaderfrom here.I might add that it might be helpful to create objects to represent the various pieces of data you need to store. That object can then make it easier to deal with the data, saving it to a database etc.
Parsing the string (this assumes the files are all the same exact format, i.e. same lines of information).
Keep track of what line you are on.