Hello there,
I tried to export a MySQL Table to a .csv but there is something that isn't working and I can't find out what it is. I first tried to get the dates saved into a DataTable and then export them to the .csv using this function:
* The error: "
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1" at sda.Fill(data);
<pre>public void CreateCSVFile(DataTable dtDataTablesList, string strFilePath)
{
// Create the CSV file to which grid data will be exported.
StreamWriter sw = new StreamWriter(strFilePath, false);
//First we will write the headers.
int iColCount = dtDataTablesList.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dtDataTablesList.Columns[i]);
if (i < iColCount - 1)
{
sw.Write("", "");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dtDataTablesList.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();
}
What I have tried:
MySqlConnection connection = CDBAccess.GetCon;
MySqlCommand sSql = new MySqlCommand("SELECT * from mytable;", connection);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = sSql;
DataTable data = new DataTable();
sda.Fill(data);
String path = @"C:\Users\Public\Documents\MyDocument";
CreateCSVFile(data, path);