Dim ReportDoc As New ReportDocument()
'定義ReportDocument對象,可以載入報表
Dim logonInfo As New TableLogonInfo 
Dim table As table  
'ReportDoc.DataBase.Tables 記錄報表存取到哪些資料表
'因此,可以用循環語句設定每個資料表的登入參數
'每個資料表的登入參數都放在LogOnInfo屬性中,為了設定方便
'把這個屬性放到變量logonInfo中
ReportDoc.Load("C:\Rpts\publish.rpt")
For Each table IN ReportDoc.DataBase.Tables
        logonInfo= table.LogonInfo
         with logonInfo.connectioninfo
          .serverName= "Localhost"
          .Databasename= "pubs"
          .UserID= "sa"
          .Password=""
        End With
'設定登入參數
        table.applyLogonInfo(logonInfo)
'提交參數
next tableCrviewer.reportsource= reportDoc
貼一段VB.net代碼比較清楚.
如果存取的資料表在不同的資料庫,則須對不同的資料表設置不同的登入參數
希望可以幫到你

解决方案 »

  1.   

    先谢谢楼上的回复(小弟盯着屏幕看了一上午盼穿秋水)
    crConnectionInfo.ServerName = "test";   //physical server name
    crConnectionInfo.DatabaseName = "Pubs";
    crConnectionInfo.UserID = "sa";
    //crConnectionInfo.Password = "admin";
    //Get the table information from the report
    crDatabase = crReportDocument.Database;
    crTables = crDatabase.Tables;
    //Loop through all tables in the report and apply the connection
    //information for each table.
    for (int i = 0; i < crTables.Count; i++)
    {
    crTable = crTables [i];
    crTableLogOnInfo = crTable.LogOnInfo;
    crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
    crTable.ApplyLogOnInfo(crTableLogOnInfo);
    }
    //Set the viewer to the report object to be previewed.
    crystalReportViewer1.ReportSource = crReportDocument;
    crDatabase = crReportDocument2.Database;
    crTables = crDatabase.Tables;
    for (int i = 0; i < crTables.Count; i++)
    {
    crTable = crTables [i];
    // MessageBox.Show(crTable.Name.ToString());
    // s1=s1+crTable.Name.ToString();
    crTableLogOnInfo = crTable.LogOnInfo;
    crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
    crTable.ApplyLogOnInfo(crTableLogOnInfo); }
    crystalReportViewer2.ReportSource = crReportDocument2;
    }那么现在有两个crystalReportViewer,这时设置的时候分不清楚到底table是属于哪个crReportDocument,岂不是就发生混淆了?我测试以后crReportDocument1就不能正常显示了
      

  2.   


     Catter(钓鱼爱好者) 的代码
    For Each table IN ReportDoc.DataBase.Tables
    ……
    next table
    Crviewer.reportsource= reportDoc
    我还是看不太明白,reportDoc和table发生关联的地方我调试代码的时候,觉得table 和reportDoc应该是有关联的,可是现在又看不出具体在哪里关联的,难道是c#自动处理的么?
    小弟初看水晶报表没几天,不太明白,高手指点一下
      

  3.   

    table 跟 reportDoc是相關的,table是ReportDoc.DataBase.Tables
    的Table
        必須注意報表中的每個數據表都要有登入參數