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

-

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

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

 

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

 要使用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水晶报表(显示图片)

你可能感兴趣的:(SharePoint)