开发者

how to arrange downloaded information to fit into rows?

开发者 https://www.devze.com 2023-04-07 03:27 出处:网络
SqlConnection connection = new SqlConnection(@\"Data Source=localhost\\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true\");
SqlConnection connection = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true");
SqlCommand Command = connection.CreateCommand();

SqlDataReader SQLRD;

Command.CommandText = "Select * from Attendance";

connection.Open();
SQLRD = Command.ExecuteReader();

string data = "";

while (SQLRD.Read())
{
    data += SQLRD[0].ToString()+  "\n";
    data += SQLRD[1].ToString(开发者_StackOverflow) + "\n";
    data += SQLRD[2].ToString() + "\n";
    data += SQLRD[3].ToString() + "\n";
    data += SQLRD[4].ToString() + "\n";
    data += SQLRD[5].ToString() + "\n";
    data += SQLRD[6].ToString() + "\n";
    data += SQLRD[7].ToString() + "\n";
}

SQLRD.Close();
connection.Close();

string filename = @"C:\download.csv";//specified location
FileStream fs = new FileStream(filename,FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(data);
sw.Flush();
sw.Close();
fs.Close();

This is what i have done so far. currently upon clicking on download all information appear in 1 single column and is not separated into rows. I need to know how can i arrange them into rows. Also can i also know how am i to display a dialog box when the user clicks on download. Currently the file is just stored in the specified location?


When exporting to .csv format, you want to put commas (,) between the columns, and line returns (\n) between the rows. Currently, you are putting a line return between every single column. Try something like this:

while (SQLRD.Read())
{
     data += SQLRD[0].ToString() + ",";
                   //              ^^^ note the change from '\n' to ','
     data += SQLRD[1].ToString() + ",";
     data += SQLRD[2].ToString() + ",";
     ...
     data += SQLRD[7].ToString(); // final column doesn't need a ','
     data += "\n"; // final line separator for the entire row
}

Best regards,

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号