我正在尝试将数据从datagridview导出到mysql的csv。我希望每一行都有一些额外的信息。
mysql查询: 从wlist中选择车辆登记
我的csv输出:
YV07WMU
我需要像
这样的输出YV07WMU; GBR; 2014年8月20日; - 这里我需要添加GBR;和今天的约会;在每一行的末尾。
这是我的代码:我不会在哪里出错。
MySqlCommand myCommand = new MySqlCommand();
myCommand.Connection = conn;
myCommand.CommandText = "select VehicleRegistration from blacklist";
//myCommand.Connection = myConn;
DataTable data = new DataTable();
MySqlDataAdapter myAdapter = new MySqlDataAdapter(myCommand);
//myAdapter.SelectCommand = myCommand;
myAdapter.Fill(data);
dataGridView1.DataSource = data;
this.dataGridView1.Refresh();
if (dataGridView1.RowCount > 0)
{
string value = "";
DataGridViewRow dr = new DataGridViewRow();
StreamWriter swOut = new StreamWriter("C:/a/blist.csv");
//write header rows to csv
for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
{
if (i > 0)
{
// swOut.Write(",");
}
// swOut.Write(dataGridView1.Columns[i].HeaderText);
}
swOut.WriteLine();
//write DataGridView rows to csv
for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++)
{
if (j > 0)
{
swOut.WriteLine(" ");
}
dr = dataGridView1.Rows[j];
for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
{
if (i > 0)
{
swOut.WriteLine(",");
}
value = dr.Cells[i].Value.ToString();
//replace comma's with spaces
value = value.Replace(',', ' ');
//replace embedded newlines with spaces
value = value.Replace(Environment.NewLine, " ");
swOut.Write(value);
}
}
swOut.Close();
}
conn.Close();
}