如何使用Asp.Net读取文本Csv文件 [英] How Do I Read A Text Csv File Using Asp.Net

查看:102
本文介绍了如何使用Asp.Net读取文本Csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何阅读文本csv文件并使用Asp.net存储在表格中





plzz帮助我..



数据很大

how do i read a text csv file and store in table using Asp.net


plzz help me ..

well the data is large

推荐答案

请参阅:



用C#读取和写入CSV文件 [ ^ ]

在.NET中读取CSV文件? [ ^ ]

如何从c#中读取csv文件中的数据 [ ^ ]

使用C#读取CSV文件 [ ^ ]



祝你好运。
Please Refer :

Reading and Writing CSV Files in C#[^]
Reading CSV files in .NET?[^]
How to read data from csv file in c#[^]
Read CSV file using C#[^]

Good luck.


查看此链接。在处理之前请记住,您需要将文件上传到网络服务器。



使用C#读取CSV文件 [ ^ ]



http://www.c-sharpcorner.com/UploadFile/d3e4b1/saving-reading-uploading-downloading-mechanism-of- csv-fil / [ ^ ]
check this link's. Remember before you process you need to upload the file to web server.

Read CSV file using C#[^]

http://www.c-sharpcorner.com/UploadFile/d3e4b1/saving-reading-uploading-downloading-mechanism-of-csv-fil/[^]


                //Step -1) Read the file from local  and save to Datatable 


                DataTable dt = new DataTable();
                string line = null;
                int i = 0;

                //Pass the file  
                using (StreamReader sr = File.OpenText(Server.MapPath("~/temp/table1.csv")))
                {
                    //Read line by line from CSV file
                    //Save the line to  string  and check for null
                    while ((line = sr.ReadLine()) != null)
                    {
                        //If not null then split with "," comma
                        string[] data = line.Split(',');
                        if (data.Length > 0)
                        {
                            //if Array length is >0  and at initial value first add column to datatable
                            if (i == 0)
                            {
                                foreach (var item in data)
                                {
                                    dt.Columns.Add(new DataColumn());
                                }
                                i++;
                            }
                            //Create DataRow  instance 
                            DataRow row = dt.NewRow();
                            //save all array to DataRow ItemArray collection
                            row.ItemArray = data;

                            dt.Rows.Add(row);
                        }
                    }
                }


//Step-2 ) If DataTable is not null then insert this Datatable to Database using BulkCopy
       
          //After saving csv file to DataTable  insert data using SqlBulkCopy
                if (dt != null && dt.Rows.Count > 0)
                {

                    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlBulkCopy"].ConnectionString))
                    {
                        cn.Open();
                        using (SqlBulkCopy copy = new SqlBulkCopy(cn))
                        {

                            copy.ColumnMappings.Add(0, 1);
                            copy.ColumnMappings.Add(1, 2);
                            copy.ColumnMappings.Add(2, 3);
                            copy.ColumnMappings.Add(3, 4);
                            copy.ColumnMappings.Add(4, 5);

                            copy.DestinationTableName = "tablename";
                            copy.WriteToServer(dt);

                        }
                    }

                }


这篇关于如何使用Asp.Net读取文本Csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆