Created
March 4, 2019 22:13
-
-
Save karoltheguy/fc701db977e1004089c052607aab65c5 to your computer and use it in GitHub Desktop.
Simple Flat File to Datatable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static DataTable Parsing(string path, string[] delimiter, bool HasHeader, bool HasFieldsinQuotes) | |
{ | |
DataTable dt = new DataTable(); | |
using (TextFieldParser reader = new TextFieldParser(path)) | |
{ | |
reader.TextFieldType = FieldType.Delimited; | |
reader.SetDelimiters(delimiter); | |
reader.HasFieldsEnclosedInQuotes = HasFieldsinQuotes; | |
if (HasHeader) | |
{ | |
string[] columns = reader.ReadFields(); | |
foreach (var column in columns) | |
{ | |
dt.Columns.Add(column); | |
} | |
} | |
while (!reader.EndOfData) | |
{ | |
string[] data = reader.ReadFields(); | |
for (int i = 0; i < data.Length; i++) | |
{ | |
if (data[i] == "") | |
data[i] = null; | |
} | |
dt.Rows.Add(data); | |
} | |
} | |
return dt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment