n);//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, Pass
word 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 = "
." + crTable.Name; } //Set the ReportSource of the Crystal