gpt4 book ai didi

c# - 将数据库中的所有详细信息存储到一个文件中,然后下载该文件

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:52 25 4
gpt4 key购买 nike

protected void btnDownload_Click(object sender, EventArgs e)
{
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();
data += SQLRD[1].ToString();

}

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

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

这就是我目前所拥有的。我想将上述查询中的所有数据存储在一个文件中。将下载此文件。

最佳答案

protected void btnDownload_Click(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true");

MySqlCommand myCommand = myConn.CreateCommand();
MySqlDataReader SQLRD;
myCommand.CommandText = "SELECT * FROM Attendance";
connection.Open();
SQLRD = myCommand.ExecuteReader();
string data="";
while (SQLRD.Read())
{
data += "Row data, arrange how you want";//SQLRD[0].Tostring();-->first coloum
}
SQLRD.Close();
connection.Close();

string filename = "F:\file1.txt"; //with path
FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(data);
sw.Flush();
sw.Close();
fs.Close();
}

编辑代码:只需复制粘贴到您的代码中并更改文件名路径

            MySqlCommand Command = connection.CreateCommand();
connection.Open();
//SqlCommand Command = new SqlCommand();
MySqlDataReader 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()+"\n";

}

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

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

关于c# - 将数据库中的所有详细信息存储到一个文件中,然后下载该文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7494611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com