将日期从mysql表导出到.CSV [英] Exporting dates from mysql table to .CSV
问题描述
你好,
我试图将MySQL表导出到.csv但是有些东西不起作用我无法找出什么它是。我首先尝试将日期保存到DataTable中,然后使用此函数将它们导出到.csv:
*错误:
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();
}
我的尝试:
MySqlConnection连接= CDBAccess.GetCon;
MySqlCommand sSql = new MySqlCommand(SELECT * from mytable;,connection);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = sSql;
DataTable data = new DataTable();
sda.Fill(数据);
String path = @C:\Users\Public \Documents\MyDocument;
CreateCSVFile(data,path);
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);
推荐答案
这将是解决方案:
This would be the solution:
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable data = new DataTable();
sda.Fill(data);
BindingSource aSource = new BindingSource();
aSource.DataSource = data;
dataGridView1.DataSource = aSource;
sda.Update(data);
StringBuilder sb = new StringBuilder();
string[] columnNames = data.Columns.Cast
这篇关于将日期从mysql表导出到.CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!