如何在txt文件中插入读取四个值的数据库?

时间:2015-09-06 08:58:11

标签: c# model-view-controller

我正在使用c#MVC项目。 我有一个客户类和客户表。我有一个包含四个参数namesurnamephoneaddress的插入函数。我想读取.txt文件行line和split with“,”并使用此Insert函数,但我不知道如何创建算法。

 static void AddCustomer(string Name, string Surname, string Phone, string Address)
    {
        using (var session = NHibernateHelper.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {
                var customer = new Customer
                {
                    Name = Name,
                    Surname = Surname,
                    Phone = Phone,
                    Address = Address,
                };
                session.Save(customer);
                transaction.Commit();
            }
        }
    }



            while ((line = file.ReadLine()) != null)
            {
                string text = file.ReadToEnd();
                string[] lines = text.Split(',');
                for (int i = 0; i < lines.Length; i++)
                {
                    //HOW CAN I USER ADDCUSTOMER()
                }  

                counter++;
            }

2 个答案:

答案 0 :(得分:2)

你几乎得到了它。假设fileStreamReader,您可以在逗号上拆分当前行,并将单独的部分传递给AddCustomer方法:

while ((line = file.ReadLine()) != null)
{
    // Split the line on comma
    var lineParts = line.Split(',');

    string name = lineParts[0];
    string surname = lineParts[1];
    string phone = lineParts[2];
    string address = lineParts[3];

    AddCustomer(name, surname, phone, address);
}

请注意,这根本不会进行错误检查(如果给定行中没有逗号,lineParts[1]会爆炸)并且这是一种解析CSV的错误方法(如果数据包含逗号,地址倾向于做,它不能正常工作)。使用CSV解析库。

请参阅Parsing CSV files in C#, with header以及有关CSV的大量其他问题,建议您使用FileHelpers library。与CSV文件进行映射的类将如下所示:

[DelimitedRecord(",")]
[IgnoreEmptyLines()]
public class MyProduct
{
    [FieldOrder(0)]
    public string Name { get; set; }

    [FieldOrder(1)]
    public string Surname { get; set; }

    [FieldOrder(2)]
    public string Phone { get; set; }

    [FieldOrder(3)]
    public string Address { get; set; }
}

读取文件的代码:

var engine = new FileHelperEngine<CustomerCsvRecord>();
CustomerCsvRecord[] customers = engine.ReadFile(fileName);

foreach (var customer in customers)
{
    AddCustomer(customer.Name, customer.Surname, customer.Phone, customer.Address);
}

答案 1 :(得分:0)

这将完成你的工作。

        string fileContent = System.IO.File.ReadAllText("YOUR_FILE_PATH");

        //assumeing each customer record will be on separate line
        string[] lines = fileContent.Split(new string [] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

        foreach (string line in lines)
        {
            //assuming a single line content will be like this "name,surname,phone,address"
            string[] items = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            Customer cust = new Customer();
            cust.Name = items[0];
            cust.Surname = items[1];
            cust.Phone = items[2];
            cust.Address = items[3];

            //now use this 'cust' object
        }