Hi telldurges,
Refer below sample code to read CSV and generate Table and insert script.
Refer below article to replace Server.MapPath in ASP.Net Core.
Namespaces
using System.Data;
using System.IO;
C#
string query = "";
string csvPath = Server.MapPath("~/Uploads/Sample.csv");
DataTable dt = new DataTable();
string csvData = File.ReadAllText(csvPath);
string fileName = Path.GetFileNameWithoutExtension(csvPath);
bool firstRow = true;
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
if (firstRow)
{
query += "IF OBJECT_ID('[dbo].[" + fileName + "]', 'U') IS NULL ";
query += "BEGIN ";
query += Environment.NewLine;
query += " CREATE TABLE [dbo].[" + fileName + "](";
foreach (string cell in row.Split(','))
{
dt.Columns.Add(cell.Trim());
query += cell.Trim() + " VARCHAR(MAX),";
}
query = query.TrimEnd(',');
query += ")";
query += Environment.NewLine;
query += "END";
firstRow = false;
}
else
{
query += Environment.NewLine;
query += "INSERT INTO [dbo].[" + fileName + "] VALUES (";
dt.Rows.Add();
int i = 0;
foreach (string cell in row.Split(','))
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Trim();
query += "'" + cell.Trim() + "',";
i++;
}
query = query.TrimEnd(',');
query += ")";
}
}
}