Read CSV file and insert to LocalDB (asp.net MVC)

守給你的承諾、 提交于 2021-01-29 05:52:08

问题


I'm trying out a project with ASP.Net MVC and have a large CSV file that I want to save to the LocalDB.

I have been following this tutorial (and the ones before that are about MVC): https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string

Now I want to add data to this database that I have set up and I would like to read this data from a csv file and then save it to my database.

I have tried this: https://www.aspsnippets.com/Articles/Upload-Read-and-Display-CSV-file-Text-File-data-in-ASPNet-MVC.aspx but when I try to upload my file I get an error that my file is too large?

I would love it if it could be automated so that when I start my application the database will be populated with the data from my csv file (and if it already is populated it will not do it again) or just some way of coding so that I can add the data from my csv file to the database (LocalDB).

    protected override void Seed(ProductsDBContext context)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        string resourceName = "WebbApplication.App_Data.SeedData.price_detail.csv";
        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        {
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                CsvReader csvReader = new CsvReader(reader);
                var products = csvReader.GetRecords<PriceDetail>().ToArray();
                context.PriceDetails.AddOrUpdate(c => c.PriceValueId, products);
            }
        }
    }

回答1:


Your second link includes the following line:

string csvData = System.IO.File.ReadAllText(filePath);

If you are getting an Out of Memory Exception, then you should not load the entire file into memory at once - i.e. do not read all of the text.

The StreamReader has a built-in function to handle this.

System.IO.StreamReader file = new System.IO.StreamReader("WebbApplication.App_Data.SeedData.price_detail.csv"); 
while((line = file.ReadLine()) != null)  
{  
    System.Console.WriteLine(line);
    //Replace with your operation below
}

Potentially the same problem solved at this question.




回答2:


With Cinchoo ETL - an open source library, you can bulk load CSV file into sqlserver with few lines of code.

using (var p = new ChoCSVReader(** YOUR CSV FILE **)
     .WithFirstLineHeader()
    )
{
    p.Bcp("** ConnectionString **", "** tablename **");
}

For more information, please visit codeproject article.

Hope it helps.



来源:https://stackoverflow.com/questions/54688699/read-csv-file-and-insert-to-localdb-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!