Sharepoint学习笔记---如何在Sharepoint2010网站中整合Crystal Report水晶报表(报表登录处理)...

-

在Sharepoint中加载水晶报表时我们常会遇到跳出水晶报表的登录界面,并且有时我们加载的报表可能会使用不同的数据库联接,而我们又需要在同一界面上对不同的报表进行处理。

 

一、为去掉这个登录界面并实现统一处理不同数据库联接的报表,我们写下如下模块

 要使用ConfigurationManager记得先引用  using System.Configuration; 
  protected   void  LoginReportDataBase(CrystalDecisions.CrystalReports.Engine.ReportDocument report)
        {
            
string  serverName1  =  ConfigurationManager.AppSettings[ " CrDBServerName1 " ];
            
string  userId1  =  ConfigurationManager.AppSettings[ " CrDBUserID1 " ];
            
string  passWord1  =  ConfigurationManager.AppSettings[ " CrDBPassWord1 " ];
            
string  dataBaseName1  =  ConfigurationManager.AppSettings[ " CrDBbaseName1 " ];


            
string  serverName2  =  ConfigurationManager.AppSettings[ " CrDBServerName2 " ];
            
string  userId2  =  ConfigurationManager.AppSettings[ " CrDBUserID2 " ];
            
string  passWord2  =  ConfigurationManager.AppSettings[ " CrDBPassWord2 " ];
           
string  dataBaseName2  =  ConfigurationManager.AppSettings[ " CrDBbaseName2 " ];


            
// Set Database Logon to main report 
             foreach  (CrystalDecisions.Shared.IConnectionInfo connection  in  report.DataSourceConnections)
            {
                
if  (connection.DatabaseName  ==  dataBaseName1  &&  connection.ServerName  ==  serverName1)
                {
                    connection.SetLogon(userId1 , passWord1);
                }
                
if  (connection.DatabaseName  ==  dataBaseName2  &&  connection.ServerName  ==  serverName2)
                {
                    connection.SetLogon(userId2, passWord2);
                }
               
            }

            
// Set Database Logon to subreport 
             foreach  (CrystalDecisions.CrystalReports.Engine.ReportDocument subreport  in  report.Subreports)
            {
                
foreach  (CrystalDecisions.Shared.IConnectionInfo connection  in  subreport.DataSourceConnections)
                {
                     
if  (connection.DatabaseName  ==  dataBaseName1  &&  connection.ServerName  ==  serverName1)
                {
                    connection.SetLogon(userId1 , passWord1);
                }
                
if  (connection.DatabaseName  ==  dataBaseName2  &&  connection.ServerName  ==  serverName2)
                {
                    connection.SetLogon(userId2, passWord2);
                }
                }
            }
        } 

 

当然,我们需要在网站的ApplicationSetting中添加如下设置,来保证我们在后台代码中获取我们需要取得配置信息:

< appSettings >
    
< add  key ="CrDBServerName1"  value ="数据库服务器1"   />
    
< add  key ="CrDBbaseName1"  value ="你的数据库名1"   />
    
< add  key ="CrDBUserID1"  value ="sa1"   />
    
< add  key ="CrDBPassWord1"  value ="sa1Pwd1"   />
   
< add  key ="CrDBServerName2"  value ="数据库服务器2"   />
    
< add  key ="CrDBbaseName2"  value ="你的数据库名2"   />
    
< add  key ="CrDBUserID2"  value ="sa2"   />
    
< add  key ="CrDBPassWord2"  value ="sa1Pwd2"   />
</ appSettings >

准备工作完成后,调用我们的登录处理模块:

            ReportDocument crdoc  =   new  ReportDocument();
            
string  retLoadStr  =   "" ;
            
try
                {
                    crdoc.Load(
" ~/Testtemp/MyCrystalReport.rpt " );
                    
// crdoc.SetDatabaseLogon("sa", "sapwd111", "cryDbServer", "SalesDetails");
                    LoginReportDataBase(crdoc);
                    
this .CrystalReportViewerStm.ReportSource  =  crdoc;
                    CrystalReportViewerStm.HasRefreshButton 
=   true ;
                    CrystalReportViewerStm.AutoDataBind 
=   true ;
                    
this .CrystalReportViewerStm.DataBind();
                    CrystalReportViewerStm.BestFitPage 
=   true ;
                    retLoadStr 
=   " OK " ;
                }
                
catch  (Exception ex)
                {
                    retLoadStr 
=  ex.ToString();
                }

 运行一下,发现登录界面没有了,水晶报表出来了。

二、如果要在运行时自动从旧的数据库服务器和数据库切换新的数据库服务器和新的数据库

参考采用如下代码:

        protected   void  ShiftLoginReportDataBase()
        {
            ReportDocument crdoc 
=   new  ReportDocument();
            
string  oldServerName  =   " OldServer " ;
            
string  newServerName  =   " NewServer " ;
            
string  oldDatabaseName  =   " OldDatabase " ;
            
string  newDatabaseName  =   " NewDatabase " ;
            
string  userID  =   " UserID " ;
            
string  password  =   " Password " ;
            crdoc.Load(
" ~/Testtemp/MyCrystalReport.rpt " );
            
//  Change the server name and database in main reports 
             foreach  (CrystalDecisions.Shared.IConnectionInfo connection  in  crdoc.DataSourceConnections)
            {
                
if  ((String.Compare(connection.ServerName, oldServerName,  true ==   0 &&  (String.Compare(connection.DatabaseName, oldDatabaseName,  true ==   0 ))
                {
                    
//  SetConnection can also be used to set new logon and new database table
                    crdoc.DataSourceConnections[oldServerName, oldDatabaseName].SetConnection(newServerName, newDatabaseName, userID, password);
                }
            }
            
//  Change the server name and database in subreport
             foreach  (CrystalDecisions.CrystalReports.Engine.ReportDocument subReport  in  crdoc.Subreports)
            {
                
foreach  (CrystalDecisions.Shared.IConnectionInfo connection  in  subReport.DataSourceConnections)
                {
                    
if  ((String.Compare(connection.ServerName, oldServerName,  true ==   0 &&  (String.Compare(connection.DatabaseName, oldDatabaseName,  true ==   0 ))
                    {
                        
//  SetConnection can also be used to set new logon and new database table
                        subReport.DataSourceConnections[oldServerName, oldDatabaseName].SetConnection(newServerName, newDatabaseName, userID, password);
                    }
                }
            }
        }

 

相关链接: Sharepoint学习笔记---如何在Sharepoint2010网站中整合Crystal Report水晶报表(显示数据 一)

相关链接: Sharepoint学习笔记---如何在Sharepoint2010网站中整合Crystal Report水晶报表(显示数据 二)

相关链接: Sharepoint学习笔记---如何在Sharepoint2010网站中整合Crystal Report水晶报表(显示图片)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值