将文本文件中的单个记录插入3个表中

时间:2013-12-19 06:26:05

标签: c# asp.net sql-server-2008

我有一个包含70行的生成文本文件。这是来自我的合作伙伴“遗产”平台的网络表单,这是通过电子邮件发送给我的文本文件:

Name
First Name
First Name + Name
Age
Telephone
City
Country
Email
Website
Profession
Etc
Etc 2
...

我希望使用C#和ASP.NET将此文本文件“拆分”到我的MS-SQL数据库中的3条记录中。

3个表是这样的:

Client_General_Information
Client_Private_Information
Client_Request_Problem

我想针对特定的表格和字段定位特定的行。示例:

Line 1 goes in Table 1 (Name field)
Line 2 goes in Table 1 (First name field)
Line 3 goes in Table 1 (...)
Line 4-5-6 goes in Table 3 ( 3 other fields)
Line 7-9 goes in Table 1 (2 other fields)
Line 10 goes in Table 2 (...)

我正在寻找一种阅读文本文件的方法,并将值插入到3个表中。文本文件将始终具有相同的行数,并且始终具有相同的顺序。有时行是空的,但它在不同文件之间变化。

所以我有我的文件阅读器,但我不知道下一步该去哪里:

        //Load our text file
        TextReader tr = new StreamReader("\\test.txt");

        int NumberOfLines = 70;

        //Make our array for each line
        string[] ListLines = new string[NumberOfLines];

        //Read the number of lines and put them in the array
        for (int i = 0; i < NumberOfLines; i++)
        {
            ListLines[i] = tr.ReadLine();
        }

        //
        tr.Close();

我应该制作3个数组还是3个列表并插入每个数组?为了清楚起见,我不想“批量插入”。每个文本文件都是唯一的客户端!

由于

1 个答案:

答案 0 :(得分:0)

创建一个单独的类,其getter和setter方法与3个表中的字段相同

Client_General_Information
Client_Private_Information
Client_Request_Problem

然后从文件中读取并将值分配给对象中的相应字段,如

...

Client_General_Information newObject = new Client_General_Information ();

int NumberOfLines = 70;

//Make our array for each line
string[] ListLines = new string[NumberOfLines];

//Read the number of lines and put them in the array
 for (int i = 0; i < NumberOfLines; i++)
 {
       ListLines[i] = tr.ReadLine();

 }
 newObject.Name=ListLines[0];
 newObject.LastName=ListLines[1];

依此类推,然后根据您对象的要求将数据插入数据库中。