asp.net水晶報表的一些問題

來源:互聯網
上載者:User
 1。第一步是添加新項CrystalReport1
2。在CrystalReport1.rpt面頁右鍵選擇:”資料庫”à”添加/刪除資料庫”出現下面對話方塊:
然後點開OLE DB(ADO)的加號,將彈出選擇資料庫的對話方塊
其中”項目資料”裡面的”當前串連”顯示的就是OLE DB(ADO)的串連,這樣就可以將表添加到右邊的”選定的表”中了(切記:不要既從”OLE DB” 中選表或命令又從”當前串連”中選,如果你用了資料集,也是一樣的道理,否則全提示出”登陸失敗”的錯誤!)
3。欄位資料管理器裡面”資料庫欄位”裡把欄位直接拖到CrystalReport.rpt裡面了.布置好布局.
4。回到WebForm1.aspx頁面,選擇控制項CrystalReportViewer將其放到頁面上。
5。代碼
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
在WebForm1.aspx.cs的Form_Load裡面添加代碼:
TableLogOnInfo logOnInfo = new TableLogOnInfo();
              ReportDocument rpdMy0= new ReportDocument();
              logOnInfo.ConnectionInfo.ServerName = "NNN";
              logOnInfo.ConnectionInfo.Password="dong";
              logOnInfo.ConnectionInfo.DatabaseName="Lab";
              logOnInfo.ConnectionInfo.UserID = "sa";
 
              String path = Server.MapPath("CrystalReport2.rpt");
              rpdMy0.Load(path);
              rpdMy0.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
              CrystalReportViewer1.ReportSource = rpdMy0;
其中:
TableLogOnInfo類是: 提供屬性,以便檢索和設定表串連選項.它有一個串連的屬性ConnectionInfo,用來設定與資料庫的串連.如上面的ServerName等.產生一個報表文檔對象將前面我們做好的那個CrystalReport1.rpt賦給它.在這裡要注意的是Database屬性!然後把報表對象做為CrystalReportViewer1的資料來源.
或者,獲得資料庫的方法改為直接從資料庫獲得DataSet
using System.Data.SqlClient;
using System.Collections.Specialized;
string strConn=System.Configuration.ConfigurationSettings.AppSettings["DB"].ToString();
   using (SqlConnection conn = new SqlConnection(strConn))
   {
       SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);//strSQL是SQL語言"select * from dbo.table1"
       DataSet ds=new DataSet();
       da.Fill( ds);
       CrystalReport1 cr1 = new CrystalReport1();
       cr1.SetDataSource( ds);
       CrystalReportViewer1.ReportSource = cr1;
   }
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
在WebForm1.aspx.cs的Form_Load裡面添加代碼:
TableLogOnInfo logOnInfo = new TableLogOnInfo();
              ReportDocument rpdMy0= new ReportDocument();
              logOnInfo.ConnectionInfo.ServerName = "NNN";
              logOnInfo.ConnectionInfo.Password="dong";
              logOnInfo.ConnectionInfo.DatabaseName="Lab";
              logOnInfo.ConnectionInfo.UserID = "sa";
 
              String path = Server.MapPath("CrystalReport2.rpt");
              rpdMy0.Load(path);
              rpdMy0.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
              CrystalReportViewer1.ReportSource = rpdMy0;
其中:
TableLogOnInfo類是: 提供屬性,以便檢索和設定表串連選項.它有一個串連的屬性ConnectionInfo,用來設定與資料庫的串連.如上面的ServerName等.產生一個報表文檔對象將前面我們做好的那個CrystalReport1.rpt賦給它.在這裡要注意的是Database屬性!然後把報表對象做為CrystalReportViewer1的資料來源.
或者,獲得資料庫的方法改為直接從資料庫獲得DataSet
using System.Data.SqlClient;
using System.Collections.Specialized;
string strConn=System.Configuration.ConfigurationSettings.AppSettings["DB"].ToString();
   using (SqlConnection conn = new SqlConnection(strConn))
   {
       SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);//strSQL是SQL語言"select * from dbo.table1"
       DataSet ds=new DataSet();
       da.Fill( ds);
       CrystalReport1 cr1 = new CrystalReport1();
       cr1.SetDataSource( ds);
       CrystalReportViewer1.ReportSource = cr1;
   }
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
在WebForm1.aspx.cs的Form_Load裡面添加代碼:
TableLogOnInfo logOnInfo = new TableLogOnInfo();
              ReportDocument rpdMy0= new ReportDocument();
              logOnInfo.ConnectionInfo.ServerName = "NNN";
              logOnInfo.ConnectionInfo.Password="dong";
              logOnInfo.ConnectionInfo.DatabaseName="Lab";
              logOnInfo.ConnectionInfo.UserID = "sa";
 
              String path = Server.MapPath("CrystalReport2.rpt");
              rpdMy0.Load(path);
              rpdMy0.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
              CrystalReportViewer1.ReportSource = rpdMy0;
其中:
TableLogOnInfo類是: 提供屬性,以便檢索和設定表串連選項.它有一個串連的屬性ConnectionInfo,用來設定與資料庫的串連.如上面的ServerName等.產生一個報表文檔對象將前面我們做好的那個CrystalReport1.rpt賦給它.在這裡要注意的是Database屬性!然後把報表對象做為CrystalReportViewer1的資料來源.
或者,獲得資料庫的方法改為直接從資料庫獲得DataSet
using System.Data.SqlClient;
using System.Collections.Specialized;
string strConn=System.Configuration.ConfigurationSettings.AppSettings["DB"].ToString();
   using (SqlConnection conn = new SqlConnection(strConn))
   {
       SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);//strSQL是SQL語言"select * from dbo.table1"
       DataSet ds=new DataSet();
       da.Fill( ds);
       CrystalReport1 cr1 = new CrystalReport1();
       cr1.SetDataSource( ds);
       CrystalReportViewer1.ReportSource = cr1;
   }如果你用的是水晶報表的推模式,一般不用設定登陸資訊,但是要這樣寫:obj.SetDataSource(this.ds.Tables["tablename"]);如果你寫成了obj.SetDataSource(this.ds)就會有登陸框的。

如果你用的是水晶報表的拉模式,你就一定要寫上登陸資訊:crReportDocument = new OracleReport();   //Set the crConnectionInfo with the current values stored in the report
   crConnectionInfo = crReportDocument.Database.Tables[0].LogOnInfo.ConnectionInfo;   /* Populate the ConnectionInfo Objects Properties with the appropriate values for
   the ServerName, User ID, Password and DatabaseName. However, since Oracle
   works on Schemas, Crystal Reports does not recognize or store a DatabaseName.
   Therefore, the DatabaseName property must be set to a BLANK string. */
   crConnectionInfo.DatabaseName = "";
   crConnectionInfo.ServerName = "Your Server Name";
   crConnectionInfo.UserID = "Your User ID";
   crConnectionInfo.Password = "Your Password";   //Set the CrDatabase Object to the Report's Database
   crDatabase = crReportDocument.Database;   //Set the CrTables object to the Tables collection of the Report's dDtabase
   crTables = crDatabase.Tables;   //Loop through each Table object in the Tables collection and apply the logon info
   //specified ealier. Note this sample only has one table so the loop will only execute once
   foreach (Table crTable in crTables)
   {
    crTableLogOnInfo = crTable.LogOnInfo;
    crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
    crTable.ApplyLogOnInfo (crTableLogOnInfo);    // if you wish to change the schema name as well, you will need to set Location property as follows:
    // crTable.Location = "<new schema name>." + crTable.Name;
   }   //Set the ReportSource of the CrystalReportViewer to the strongly typed Report included in the project
   crystalReportViewer1.ReportSource = crReportDocument;

還有一點要注意:
如果你用到了子報表,一定要處理://Go through each sections in the main report and identify the subreport by name
   crSections = crReportDocument.ReportDefinition.Sections;   foreach(Section crSection in crSections)
   {
    crReportObjects = crSection.ReportObjects;
    //loop through all the report objects to find all the subreports
    foreach(ReportObject crReportObject in crReportObjects)
    {
     if (crReportObject.Kind == ReportObjectKind.SubreportObject)
     {
      //you will need to typecast the reportobject to a subreport
      //object once you find it
      crSubreportObject = (SubreportObject)crReportObject;      //open the subreport object
      crSubReportDoc = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
      //Once the correct subreport has been located pass it the
      //appropriate dataset
      if(crSubReportDoc.Name == "FirstSub")
      {
       //crSubReportDoc.Database.Tables[0].SetDataSource(ds);
       crSubReportDoc.SetDataSource(ds);
      }
     }
    }
   }
   crystalReportViewer1.ReportSource = crReportDocument;同樣crSubReportDoc.SetDataSource(ds);改為:crSubReportDoc.SetDataSource(ds.Tables["tablename"]);
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.