2016-08-05 77 views
1

我想通過從aspx調用存儲過程來導出文件,並且我想將數據保存到.txt文件。每列必須具有需要設置的特定列長度。下面是我的代碼,但是當我運行程序時,輸出只顯示列名...並且沒有數據出現。它看起來像程序沒有讀取行語句。請幫我使用aspx將數據從存儲過程導出到txt文件

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

我的代碼是'dt'由存儲過程填充? –

回答

0

我測試了你的代碼,它的工作原理應該是這樣的。問題是存儲過程AR_Ebill_claim返回零行。但是沒有錯誤,所以列名不會被導出。

我認爲問題在於參數。首先檢查它們並測試存儲過程是否在SQL Server Studio或類似程序中提供了所需的結果。

0

我已經得到了解決方案如下:)

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(); 

         string outputFilePath = Server.MapPath("~/Documents/EClaim.txt"); 

         if (File.Exists("~/Documents/EClaim.txt")) 
         { 
          File.Delete("~/Documents/EClaim.txt"); 

         } 

         SqlDataAdapter da = new SqlDataAdapter(); 
         DataTable dt = new DataTable(); 

         da.SelectCommand = cmd; 
         da.Fill(dt); 

         int[] maxLengths = new int[dt.Columns.Count]; 

         for (int i = 0; i < dt.Columns.Count; i++) 
         { 
          maxLengths[i] = dt.Columns[i].ColumnName.Length; 

          foreach (DataRow row in dt.Rows) 
          { 
           if (!row.IsNull(i)) 
           { 
            int length = row[i].ToString().Length; 

            if (length > maxLengths[i]) 
            { 
             maxLengths[i] = length; 
            } 
           } 
          } 
         } 

         using (StreamWriter sw = new StreamWriter(outputFilePath, false)) 
         { 
          //for (int i = 0; i < dt.Columns.Count; i++) 
          //{ 
          // sw.Write(dt.Columns[i].ColumnName.PadRight(maxLengths[i] + 2)); 
          //} 

          sw.WriteLine(); 

          foreach (DataRow row in dt.Rows) 
          { 
           for (int i = 0; i < dt.Columns.Count; i++) 
           { 
            if (!row.IsNull(i)) 
            { 
             sw.Write(row[i].ToString().PadRight(maxLengths[i] + 1)); 
            } 
            else 
            { 
             sw.Write(new string(' ', maxLengths[i] + 1)); 
            } 
           } 

           sw.WriteLine(); 
          } 

          sw.Close(); 
         } 

         //string filePath = "~/Documents/EBilling.txt"; 
         //Response.ContentType = "application/text"; 
         //Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\""); 
         //Response.TransmitFile(Server.MapPath(filePath)); 
         ////Response.End();       
        } 
+0

也許你可以解釋一下如何解決這個問題。這個解決方案並不明顯。 –

相關問題