上方廣告

Lady Kukki 手作糖霜餅乾

2009年9月8日 星期二

ASP.NET ReportViewr使用方式

string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
DataSet myDs = new Report1();

using (SqlConnection conn = new SqlConnection(strConnection))
{
    conn.Open();

    string strSQL = "select * from XXXX";
    using (SqlCommand cmd = new SqlCommand(strSQL, conn))
    {
        SqlDataReader myDr = cmd.ExecuteReader();

        myDs.Tables["statlist"].Load(myDr);
        myDr.Close();
        myDr.Dispose();
    }
}


//將ReportViewer1的DataSources集合清除
ReportViewer1.LocalReport.DataSources.Clear();
//匯出檔案名稱
ReportViewer1.LocalReport.DisplayName = "XXX彙總表";
//為查看器提供本地報表數據
ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report1.rdlc");
ReportViewer1.LocalReport.DataSources.Add(
new Microsoft.Reporting.WebForms.ReportDataSource("Report1_statlist", myDs.Tables["statlist"]));

//參數
//ReportParameter param = new ReportParameter("Title", "嗚嗚");
//ReportParameter[] p ={ param };
ReportParameter[] p ={ new ReportParameter("UserName", "Lawrence"),
                        new ReportParameter("SDate", "2009/09/01"),
                        new ReportParameter("EDate", "2009/09/30")};
ReportViewer1.LocalReport.SetParameters(p);