2012-04-10 9 views
0

下午好開發商, 這是我的函數生成的水晶報表,這將需要的報表名稱和ReportQuery作爲輸入參數,並會生成一個數據集,有了這個數據集我怎樣才能設計我的報告?因爲這是在運行時生成的,我怎麼能訪問到生成我ReportDesign?如何設計Crystal報表與動態數據

public void fnLoadDataToReport(string rptName, string rptQuery) 
{ 
    try 
    { 
     DataSet myDS=new DataSet(); 
//  crReportDocument.Load(Server.MapPath("Reports\" & RptName), OpenReportMethod.OpenReportByTempCopy); 
     crReportDocument.Load(Server.MapPath("Reports\\" + rptName)); 
     SqlConnection myConnection=new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString); 
     SqlCommand myCommand=new SqlCommand(rptQuery,myConnection); 
     SqlDataAdapter MyDA=new SqlDataAdapter(myCommand); 
     MyDA.Fill(myDS,"ReportTable"); 
     crReportDocument.SetDataSource(myDS); 
     crvReportGeneration.ReportSource=crReportDocument; 
     crvReportGeneration.ShowFirstPage(); 
    } 
    catch(Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
} 

任何幫助是可觀的....!

+0

嘗試使用using語句封裝SqlConnection,SqlCommand和SqlDataAdapter - http://msdn.microsoft.com/en-us/library/yh598w02.aspx – 2012-04-10 07:39:23

+0

封裝的用途是什麼?那之後該怎麼辦? – 2012-04-10 08:33:07

回答

1

所有你需要的是數據集XML模式定義到能夠設計那個報告。這可以通過下面的代碼來實現。

private void WriteSchemaToFile(DataSet thisDataSet){ 
    // Set the file path and name. Modify this for your purposes. 
    string filename="mySchema.xml"; 
    // Write the schema to the file. 
    thisDataSet.WriteXmlSchema(filename); 
} 

現在將上面創建的文件添加到您的項目。

現在在設計報告時選擇數據庫專家並選擇項目下的xml文件。並添加它(就像添加表格或視圖一樣)。

現在你是好去。其餘的步驟是相同的​​。

只需確保XSD架構的名稱,準確的數據集匹配的表名。

0

爲什麼使用DataSet中的源報表。取而代之的是,如果你使用存儲過程作爲源,它會解決你的問題,也維護將變得更加容易