Hi,
Create Table in SQL database with same column name as present in your Text file which needs to read.
Then Create a C# Console application and place below code in program.cs file
internal class Program
{
private static string SqlDBConnectionString="";
private static void SaveDataToDB(DataTable DT)
{
using (var bulkCopy = new SqlBulkCopy(SqlDBConnectionString, SqlBulkCopyOptions.KeepIdentity))
{
foreach (DataColumn col in DT.Columns)
{
bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
}
bulkCopy.BulkCopyTimeout = 600;
bulkCopy.DestinationTableName = "SqlTableName";
bulkCopy.WriteToServer(DT);
}
}
static void Main()
{
var filename = @"D:\PERSONAL\RawData" + DateTime.Now.ToString("ddMMyyyy") + ".txt";
if (File.Exists(filename))
{
var DT = new DataTable();
IEnumerable<string> TextLines = File.ReadLines(filename);
if (TextLines != null)
{
var headers = TextLines.First().Split(',');
foreach (var header in headers)
{
DT.Columns.Add(header);
}
var records = TextLines.Skip(1);
foreach (var record in records)
{
DT.Rows.Add(record.Split(','));
}
if (DT.Rows.Count > 0)
{
SaveDataToDB(DT);
}
}
else
{
Console.WriteLine("File is empty.");
}
}
}
}
And lastly schedule application as per your need using windows scheduler.
Hope this helps you.