2011-09-21 29 views
2
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(); } 

這就是我到目前爲止所做的。我想將上述查詢中的所有數據存儲在一個文件中。該文件將被下載。將數據庫中的所有詳細信息存儲到文件中,然後下載文件

+0

有什麼樣的靈魂在那裏幫忙? – Mark20

回答

1
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(); 
     } 
+0

嗨,非常感謝!然而,我仍然在「字符串文件名=」F:\ file1.txt「;」,錯誤說無法識別的轉義序列,我不明白這一點,我該怎麼去解決這個錯誤? – Mark20

+0

string filename =「F:\\ file1.txt」;或FileStream fs =新的FileStream(@filename,FileMode.Append,FileAccess.Write);任何你可以做的 – deepi

+0

我可以問最後一個問題嗎?我只能獲得一行數據,我想獲得9行數據,我該怎麼做?對不起,您在 – Mark20

0

你應該使用序列化來提供最好的服務。我將使用Active Record設計模式創建一個結構化對象。

然後,我會使用XML序列化將該對象輸出到XML對象。然後,我會將該XML保存到磁盤並稍後將其反序列化,並使用它,就好像沒有任何事情發生一樣。

請參見:How to serialize and object to XML in C# on MSDN

0

寫入到文件最簡單的方法是使用DataTable.WriteXml方法,數據表中的全部數據寫入到一個XML文件,該文件以後可以下載:

SqlDataAdapter sqlAdap = new SqlDataAdapter(query, connection); 
DataTable dt = new DataTable(); 
sqlAdap.Fill(dt); 

string filePath = Server.MapPath("~/filename.xml"); 
dt.WriteXml(filePath); 

但是如果你想寫這個數據到一些特定的格式,比如說csv,你必須遍歷所有的行,並且使用StreamWrite類,你可以將它寫入文件。下面的代碼只是給你一個想法,我沒有添加任何類型檢查或檢查null。

string filePath = Server.MapPath("~/somefile.csv"); 
System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath); 

foreach (DataRow r in dt.Rows) 
{ 
     for (int i = 0; i < dt.Columns.Count; i++) 
     { 
      sw.Write(r[i].ToString()); 
      sw.Write(","); 
     } 

     sw.WriteLine(); 
} 
sw.Close(); 
0

您可以將其導出爲DataSet磁盤這樣的:

using (var conn = new SqlConnection(@"<your conn str>")) 
{ 
    var ds = new DataSet(); 
    using (var invoiceCmd = conn.CreateCommand()) 

    //One of these per table to export 
    using(var invoiceAdapter = new SqlDataAdapter(invoiceCmd)) 
    { 
     invoiceCmd.CommandText = "SELECT * FROM Attendance"; 
     invoiceAdapter.Fill(ds, "Attendance"); 
    } 

    ds.WriteXml(@"C:\temp\foo.xml"); 
} 
1

你的代碼,以將數據保存到文件根據您所需的文件格式將發生變化。保存文件後,請使用HttpResponse.TransmitFile將文件推送到瀏覽器。例如,模板代碼將是

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

    string query = "SELECT * FROM Attendance"; 

    // Fetch data using data reader or data adapter 
    ... 

    // Save the data to file in required format 
    string filePath; 
    .... 

    // Push the file from response 
    Response.ContentType = "text/csv"; // set as per file type e.g. text/plain, text/xml etc 
    Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv"); // will prompt user for save file dialog, use inline instead of attachment to suppress the dialog 
    Response.TransmitFile(filePath); 
} 

查看其他的答案爲代碼在某些格式的文件中存儲諸如XML/CSV。

相關問題