0
數據首先被提取到DataTable中,然後DataTable將被導出到可以在記事本中查看的Text文件。將數據從SQL Server導出到C#中的文本文件(保存到特定的文件夾)
但是,我不知道如何使代碼工作,爲工作保存到特定的文件夾
PSI想給該文件的動態名稱太(YEARmonthDAYhour.txt)
這是我的到目前爲止的代碼:
protected void ExportTextFile(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Select * from details"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
string txt = string.Empty;
txt += "#";
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
txt += row[column.ColumnName].ToString() + "$";
}
}
txt += "%";
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=AAAAMM-aaaammddhhmmss.txt");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(txt);
Response.Flush();
Response.End();
}
}
}
}
}
預期輸出:
'#$ InfofromSQL $ InfofromSQl $ InfofromSQL ......%'(不帶「'「)
數據相差$。
它需要是一個文本文件嗎?如果沒有,我有一個CSV解決方案(CSV文件可以在記事本和Excel中打開)。 CSV也更容易閱讀。 – Paul
需要一個文本文件:( – Bigby
您可以編輯您的文章,並顯示例如預期輸出 – Paul