Table Name in SqlServer DataBase: MyTable
Csv File location : @"d:MyFile.csv"
How to Copy the CSV file "@"d:MyFile.csv" to "MyTable" a table that exists in SQL server database using C# Console application?!!!
I have used following C# code to export from database to CSV file. But how to do the reverse task?!!!
string strConn = "Data Source=MYSERVER;Initial Catalog=Master;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("select * from QuickBook", conn);
DataSet ds = new DataSet();
da.Fill(ds, "QB");
DataTable dt = ds.Tables["QB"];
StreamWriter sw = new StreamWriter(@"d:MyFile.csv", false);
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
}