读者朋友:
您好,你用下面的的代码动态连一下水晶报表就可以了:
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
//这里必须事先申明一个ReportDocument对象 Report,同时加载数据报表
ReportDocument oRpt = new ReportDocument();
//获取.rpt文件真实路径
string path;
path="crystal\\cr1.rpt";
oRpt.Load (path);
//从web.config中获取logOnInfo参数信息
string a,b,c,d;
//获取ServerName
a=System.Configuration .ConfigurationSettings .AppSettings ["servername"];
//获取DatabaseName
b=System.Configuration .ConfigurationSettings .AppSettings ["database"];
//获取UserId
c=System.Configuration .ConfigurationSettings .AppSettings ["userid"];
//获取password
d=System.Configuration .ConfigurationSettings .AppSettings ["pass"];
//设置logOnInfo参数
logOnInfo.ConnectionInfo.ServerName = a;
logOnInfo.ConnectionInfo.DatabaseName = b;
logOnInfo.ConnectionInfo.UserID = c;
logOnInfo.ConnectionInfo.Password = d;
oRpt.Database .Tables [0].ApplyLogOnInfo (logOnInfo);
//建立.rpt文件与CryStalReportviewer文件之间的连接
CrystalReportViewer1.ReportSource = oRpt;
注意:添加一下using CrystalDecisions.Shared;这个命名空间。