Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# read csv file save to database dynamically

  private readonly DataTable dt = new DataTable();

        /// <summary>
        /// Read CSV file and save to DataTable dynamically
        /// </summary>
        /// <param name="xmlFilePath"></param>
        /// <param name="csvFilePath"></param>
        /// <returns></returns>
        public DataTable Read(string xmlFilePath, string csvFilePath)
        {
            try
            {
                var headrs = File.ReadLines(csvFilePath).First().Split(',');
                foreach (var item in headrs)
                {
                    dt.Columns.Add(item.ToString(), typeof(string));
                }

                string[] lines = File.ReadAllLines(csvFilePath);
                foreach (string line in lines.Skip(1))
                {
                    string[] columns = line.Split(',');
                    dt.Rows.Add(columns);

                }

                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }	
Comment

PREVIOUS NEXT
Code Example
Csharp :: get all controlswpf 
Csharp :: mydata api .net 
Csharp :: c# check if file is zero bytes 
Csharp :: duplicate global system runtime versioning targetframeworkattribute 
Csharp :: SETTING UP ARRAY FOR TEST SCORES IN C# 
Csharp :: c# uri to string 
Csharp :: c# timestamp 
Csharp :: c# variable 
Csharp :: c# alert message 
Csharp :: Known Folders C# 
Csharp :: Service Locator, Unity 
Csharp :: DateTime2 error in EF Blazor Web Application 
Csharp :: how to change the scale of a gameobject in unity 
Csharp :: Maximize Print Preview 
Csharp :: activeNetworkInfo depricated 
Csharp :: how to seperate front of decimal and back of decimal in C# 
Csharp :: c# half hour dropdown list 
Csharp :: c# default parameter 
Csharp :: c# string with double quotes inside 
Csharp :: add new page itext 7 
Csharp :: vb.net convert int32 into boolean array stack overflow 
Csharp :: retrive the last record dynamics 365 by c# 
Csharp :: c# get buttons row and column in grid 
Csharp :: C# JOSN Array Conversion 
Csharp :: closing main window after clicking on a button that opens another window in wpf 
Csharp :: read dxf file c# 
Csharp :: How to solve error in ExecuteNonQuery() in asp.net 
Csharp :: jtoken value is not exact double 
Csharp :: asp.net render control to string 
Csharp :: internet connection sharing 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =