在Sql server中有两个Database,“db1”和“db2”,两个数据库除了名称不一样,表的数量、结构等全都相同,我连接db1做了个Crystalreport,用下面代码在web上所有功能都能实现,但当我在程序中把database改成db2时,就会提示要我输入servername,databasename,uid,pwd,我百思不得其解。
[code]
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
//这里必须事先申明一个ReportDocument对象 Report,同时加载数据报表
oRpt = new ReportDocument();
//获取.rpt文件真实路径
string path;
path = Application["rptpath"].ToString() + "\\resource\\reports\\AgeBalance.rpt";
oRpt.Load (path);
//从web.config中获取logOnInfo参数信息
string a,b,c,d;
//获取ServerName
a="server1";
//获取DatabaseName
b="db1"; //如果db1改成db2就会提示
//获取UserId
c="sa";
//获取password
d="sa"; CrystalDecisions.CrystalReports.Engine.Sections crSections;
CrystalDecisions.CrystalReports.Engine.ReportObjects crReportObjects;
CrystalDecisions.CrystalReports.Engine.SubreportObject crSubreportObject;
CrystalDecisions.CrystalReports.Engine.Database crDatabase;
CrystalDecisions.CrystalReports.Engine.Tables crTables;
CrystalDecisions.Shared.ConnectionInfo crConnectioninfo = new ConnectionInfo();
ReportDocument crSubreportDocument;
//设置logOnInfo参数
foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in oRpt.Database.Tables)
{logOnInfo = tbl.LogOnInfo;
logOnInfo.ConnectionInfo.ServerName = a;
logOnInfo.ConnectionInfo.DatabaseName = b;
logOnInfo.ConnectionInfo.UserID = c;
logOnInfo.ConnectionInfo.Password = d;
logOnInfo.TableName = "AROBL";
tbl.ApplyLogOnInfo(logOnInfo);
}crSections = oRpt.ReportDefinition.Sections;
foreach (Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
foreach (ReportObject crReportObject in crReportObjects)
{
if (crReportObject.Kind == ReportObjectKind.SubreportObject)
{
crSubreportObject = (SubreportObject)crReportObject;
crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
crDatabase = crSubreportDocument.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crConnectioninfo.ServerName = a;
crConnectioninfo.UserID = c;
crConnectioninfo.Password = d;
crConnectioninfo.DatabaseName = b;
logOnInfo = crTable.LogOnInfo;
logOnInfo.ConnectionInfo = crConnectioninfo;
crTable.ApplyLogOnInfo(logOnInfo);
}
}}
}
[code]
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
//这里必须事先申明一个ReportDocument对象 Report,同时加载数据报表
oRpt = new ReportDocument();
//获取.rpt文件真实路径
string path;
path = Application["rptpath"].ToString() + "\\resource\\reports\\AgeBalance.rpt";
oRpt.Load (path);
//从web.config中获取logOnInfo参数信息
string a,b,c,d;
//获取ServerName
a="server1";
//获取DatabaseName
b="db1"; //如果db1改成db2就会提示
//获取UserId
c="sa";
//获取password
d="sa"; CrystalDecisions.CrystalReports.Engine.Sections crSections;
CrystalDecisions.CrystalReports.Engine.ReportObjects crReportObjects;
CrystalDecisions.CrystalReports.Engine.SubreportObject crSubreportObject;
CrystalDecisions.CrystalReports.Engine.Database crDatabase;
CrystalDecisions.CrystalReports.Engine.Tables crTables;
CrystalDecisions.Shared.ConnectionInfo crConnectioninfo = new ConnectionInfo();
ReportDocument crSubreportDocument;
//设置logOnInfo参数
foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in oRpt.Database.Tables)
{logOnInfo = tbl.LogOnInfo;
logOnInfo.ConnectionInfo.ServerName = a;
logOnInfo.ConnectionInfo.DatabaseName = b;
logOnInfo.ConnectionInfo.UserID = c;
logOnInfo.ConnectionInfo.Password = d;
logOnInfo.TableName = "AROBL";
tbl.ApplyLogOnInfo(logOnInfo);
}crSections = oRpt.ReportDefinition.Sections;
foreach (Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
foreach (ReportObject crReportObject in crReportObjects)
{
if (crReportObject.Kind == ReportObjectKind.SubreportObject)
{
crSubreportObject = (SubreportObject)crReportObject;
crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
crDatabase = crSubreportDocument.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crConnectioninfo.ServerName = a;
crConnectioninfo.UserID = c;
crConnectioninfo.Password = d;
crConnectioninfo.DatabaseName = b;
logOnInfo = crTable.LogOnInfo;
logOnInfo.ConnectionInfo = crConnectioninfo;
crTable.ApplyLogOnInfo(logOnInfo);
}
}}
}
CrystalReportViewer1.ReportSource = oRpt;
CrystalReportViewer1.DataBind();
应该不会出现你说的那种情况。你看看数据库里用户sa对两个数据库db1 和db2
的权限是否一样.
Dim dsdataSet As New DataSet()
Dim oRpt As New rptClient() '已建立的报表rptClient
请读者自行填充数据集dsdataSet
'使用“报表引擎”对象模型将填充的数据集,传递给报表
oRpt.SetDataSource(dsdataSet.Tables(0))
' 将带有数据的报表对象绑定到 Windows 窗体查看器,rptVew(CrystalReportViewer控件)
rptVew.ReportSource = oRpt
我现在不能用dataset模式,我想知道我所写的代码有什么不对的地方,用dataset模式,好像crystalreport自带的工具条不能用。
SqlConnection1.ConnectionString = "workstation id=""YT-NET"";packet size=4096;user id=sa;data source=""YT-NET"";persist " & _
"security info=True;initial catalog=db1;password=123"
完全没有问题