2013-06-25 63 views
1

我所做的只是建立在形式的ReportViewer,然後我有這樣的代碼:創建數據集動態,並傳遞到的ReportViewer

 SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\I.S\C#\billingSystem\Store.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"); 


     private void Form1_Load() 
     { 
      runRptViewer(); 
      cn.Open(); 
     } 

     private void rptGetDataset() 
     { 
      DataSet ds = new DataSet(); 
      ds.DataSetName = "dsNewDataSet"; 
      SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NewBill", cn); 
      ds.GetXmlSchema(); 
      da.Fill(ds); 
      ds.WriteXmlSchema(@"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xsd"); 
      ds.WriteXml(@"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xml"); 
     } 

     private DataTable getData() 
     { 
      DataSet dss = new DataSet(); 
      SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NewBill", cn); 
      da.Fill(dss); 
      DataTable dt = dss.Tables["NewBill"]; 
      return dt; 
     } 

     private void runRptViewer() 
     { 
      this.reportViewer2.Reset(); 
      //this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc"); 
      this.reportViewer2.LocalReport.ReportPath =(@"G:\I.S\Testoooooooo\Testoooooooo\Report1.rdlc"); 
      ReportDataSource rds = new ReportDataSource("dsNewDataSet_NewBill", getData()); 
      this.reportViewer2.LocalReport.DataSources.Clear(); 
      this.reportViewer2.LocalReport.DataSources.Add(rds); 
      //this.reportViewer2.DataBind(); 
      this.reportViewer2.LocalReport.Refresh(); 
     } 
    } 

我有兩個的ReportViewer的reportViewer1工作,但如果DB的目錄已經改變它不會工作,所以這就是爲什麼我嘗試在另一個reportViewer,即使DB的目錄改變,使其工作,我可以改變連接字符串。

的問題是該報告不顯示任何東西,我覺得在代碼中的問題:

//this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc"); 

這是一個Windows窗體所以沒有服務器,香港專業教育學院將其更改爲:

this.reportViewer2.LocalReport.ReportPath =(@"G:\I.S\Testoooooooo\Testoooooooo\Report1.rdlc"); 

,這一個不工作:

//this.reportViewer2.DataBind(); 

我無法理解這兩條線,這是否意味着創建一個數據集1.xsd和Dataset1.xml,或者只是編輯它們。 ds.WriteXmlSchema(@「G:\ I.S \ Testoooooooo \ Testoooooooo \ Dataset1.xsd」); ds.WriteXml(@「G:\ I.S \ Testoooooooo \ Testoooooooo \ Dataset1.xml」);如果可能的話,我需要從創建每一件事來編碼,這將是偉大的步驟。

回答

0

要使用報表查看器在C#的WinForm項目下面的代碼添加到一個按鈕或在礦井的Form_Load:

string strConnectionString = "Data Source=(local);Initial Catalog=Projects_DB;Integrated Security=True"; 
DataSet ds = new DataSet(); 
SqlDataAdapter da = new SqlDataAdapter(); 
SqlCommand cmd = new SqlCommand("sp_GetProject " + "'0660CAD6-6F1A-4D19-A1FD-17BF3655AC98'"); 
cmd.CommandType = CommandType.Text; 
cmd.Connection = new SqlConnection (strConnectionString); 
da.SelectCommand = cmd; 

da.Fill(ds,"DataSet1"); 

reportViewer1.ProcessingMode = ProcessingMode.Local; 
reportViewer1.LocalReport.DataSources.Clear(); 
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds.Tables[0])); 
reportViewer1.LocalReport.Refresh(); 
reportViewer1.RefreshReport(); 

假設你有一個不接受@ProjectID參數的存儲過程。如果您將這些參數與sql命令一起傳遞,則必須設置cmd.CommandType = CommandType.Text。但是,如果您不想傳遞參數,只需將commandType更改爲cmd.CommandType = CommandType.StoredProcedure

下面是本示例中使用存儲過程:

CREATE PROC [dbo].[sp_GetProject] 
    @ProjectID  nvarchar(50)=NULL 

AS 
BEGIN 

    SELECT ProjectID, ProjectName FROM Projects 
    WHERE 
    (@ProjectID IS NULL) OR (ProjectID = @ProjectID) 

END 
相關問題