使用aspx将数据从存储过程导出到txt文件

时间:2016-08-05 02:10:54

标签: c# asp.net sql-server stored-procedures data-export

我想通过从aspx调用存储过程来导出文件,我想将数据保存到protected void Page_Load(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; string str = "Server=KABS;Database=HOT;uid=sa;pwd=DDD;Connection Timeout=6000"; if (Request.QueryString["ProcessName"] != null) { using (SqlConnection con = new SqlConnection(str)) { if (Request.QueryString["ProcessName"].ToString().Equals("Ebill")) { using (SqlCommand cmd = new SqlCommand("AR_Ebill_claim", con)) { cmd.CommandType = CommandType.StoredProcedure; string compcode = null; DateTime dateFrom = DateTime.Now; DateTime dateTo = DateTime.Now; string episType = null; string debtorCode = null; if (Request.QueryString["compcode"] != null) { if (string.IsNullOrEmpty(Convert.ToString(Request.QueryString["compcode"]))) { compcode = null; } else { compcode = Convert.ToString(Request.QueryString["compcode"]); } } if (Request.QueryString["dateFrom"] != null) { DateTime dtFrom = DateTime.Parse(Request.QueryString["dateFrom"]); dtFrom.ToString("dd-MMM-yyyy"); if (dtFrom == null) { dateFrom = DateTime.Now; } else { dateFrom = dtFrom; } } if (Request.QueryString["dateTo"] != null) { DateTime dtTo = DateTime.Parse(Request.QueryString["dateTo"]); dtTo.ToString("dd-MMM-yyyy"); if (dtTo == null) { dateTo = DateTime.Now; } else { dateTo = dtTo; } } if (Request.QueryString["episType"] != null) { if (string.IsNullOrEmpty(Convert.ToString(Request.QueryString["episType"]))) { episType = null; } else { episType = Convert.ToString(Request.QueryString["episType"]); } } if (Request.QueryString["debtorCode"] != null) { if (string.IsNullOrEmpty(Convert.ToString(Request.QueryString["debtorCode"]))) { debtorCode = null; } else { debtorCode = Convert.ToString(Request.QueryString["debtorCode"]); } } cmd.Parameters.Add("@compcode", SqlDbType.VarChar, 100); cmd.Parameters["@compcode"].Value = compcode; cmd.Parameters.Add("@dateFrom", SqlDbType.SmallDateTime); cmd.Parameters["@dateFrom"].Value = dateFrom; cmd.Parameters.Add("@dateTo", SqlDbType.SmallDateTime); cmd.Parameters["@dateTo"].Value = dateTo; cmd.Parameters.Add("@episType", SqlDbType.VarChar, 40); cmd.Parameters["@episType"].Value = episType; cmd.Parameters.Add("@debtorCode", SqlDbType.VarChar, 100); cmd.Parameters["@debtorCode"].Value = debtorCode; con.Open(); //cmd.ExecuteNonQuery(); //gdBill.EmptyDataText = "No Records Found"; //gdBill.DataSource = cmd.ExecuteReader(); //gdBill.DataBind(); SqlDataAdapter da = new SqlDataAdapter(); DataTable dt = new DataTable(); da.SelectCommand = cmd; da.Fill(dt); string txt = string.Empty; if (dt.Columns.Count > 0) { foreach (DataColumn column in dt.Columns) { //Add the Header row for Text file. txt += column.ColumnName + "\t\t"; } } //Add new line. txt += "\r\n"; if (dt.Rows.Count > 0) { foreach (DataRow row in dt.Rows) { foreach (DataColumn column in dt.Columns) { //Add the Data rows. txt += row[column.ColumnName].ToString() + "\t\t"; } //Add new line. txt += "\r\n"; } } Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=E-Billing.txt"); Response.Charset = ""; Response.ContentType = "application/text"; Response.Output.Write(txt); Response.Flush(); Response.End(); Cursor.Current = Cursors.AppStarting; } } //cmd.Dispose(); con.Close(); } } } 文件。每列必须具有需要设置的特定列长度。下面是我的代码,但是当我运行程序时,输出只显示列名...而且没有数据出现。它看起来像程序没有读取行语句。请帮帮我

atom-one-theme.el

2 个答案:

答案 0 :(得分:0)

我测试了你的代码,它的工作方式应该如此。问题是存储过程AR_Ebill_claim返回零行。但是没有错误,因此列名称会被导出。

我认为问题在于参数。首先检查这些并测试存储过程是否在SQL Server studio或类似程序中提供了所需的结果。

答案 1 :(得分:0)

我已经获得了以下代码的解决方案:)

SELECT * FROM `table` WHERE CURDATE() between fromdate and todate