Skip to content

Instantly share code, notes, and snippets.

@karoltheguy
Created March 4, 2019 22:13
Show Gist options
  • Save karoltheguy/fc701db977e1004089c052607aab65c5 to your computer and use it in GitHub Desktop.
Save karoltheguy/fc701db977e1004089c052607aab65c5 to your computer and use it in GitHub Desktop.
Simple Flat File to Datatable
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