1、你建.rpt文件时,要把报表的库连接建好;
2、在代码中,建好库连接private void Page_Load(object sender, System.EventArgs e)
{
         CrystalDecisions.Shared.TableLogOnInfo logInfo = new CrystalDecisions.Shared.TableLogOnInfo();
logInfo.ConnectionInfo.ServerName = "YouServerName";
logInfo.ConnectionInfo.DatabaseName = "DatabaseName";
logInfo.TableName = "TableName";
logInfo.ConnectionInfo.UserID = "sa";
logInfo.ConnectionInfo.Password = "";
CrystalReportViewer1.ReportSource = Server.MapPath("cr_aa.rpt"); //指定报表的数据源
CrystalReportViewer1.LogOnInfo.Add(logInfo);//对参数进行赋什值 
}

解决方案 »

  1.   

    原因是你登陆sqlserver的用户有密码,最简单的方法是不设密码。
    如果设密码,就要加以下语句:        report.Load(Server.MapPath(reportname))
            Dim logOnInfo As TableLogOnInfo = New TableLogOnInfo()        Dim i As Integer = 0
            Do While i <= report.Database.Tables.Count - 1
                logOnInfo.ConnectionInfo.UserID = "sa"
                logOnInfo.ConnectionInfo.Password = "sa"
                report.Database.Tables(i).ApplyLogOnInfo(logOnInfo)
                i = i + 1
            Loop
      

  2.   

    楼上说的对!Dim tbCurrent As CrystalDecisions.CrystalReports.Engine.Table
    Dim tliCurrent As CrystalDecisions.Shared.TableLogOnInfoFor Each tbCurrent In cbsMain.Database.Tables
        tliCurrent = tbCurrent.LogOnInfo
        With tliCurrent.ConnectionInfo
            .ServerName = "localhost"
            .UserID = "ReportUser"
            .Password = "msdn"
            .DatabaseName = "Northwind"
        End With
        tbCurrent.ApplyLogOnInfo(tliCurrent)
    Next tbCurrent
      

  3.   

    多谢各位良言!试了一下,但是报表显示是空的呀!我是用VB.NET开发的!
    to: gqxm   report.Load(Server.MapPath(reportname))中report是不是定义的ReportDocument()?TO: shyuan(shyuan) cbsMain.Database.Tables中  cbsMain是?
      

  4.   

    report是定义的ReportDocument,reportname是水晶报表的名称。
    之前还要作以下引用:
    Imports System.Data
    Imports System.Data.SqlClient
    Imports CrystalDecisions.Shared
    Imports CrystalDecisions.ReportSource
    Imports CrystalDecisions.CrystalReports.EngineDim Report As ReportDocument = New ReportDocument()
      

  5.   

    建一个dataset数据集将要选的表填充,同时建一个crystalreport.rpt文件。
    绑定水晶报表的代码如下:
    Sub BindCryRpt(ByVal tableName As String)
            Dim oRpt As New CrystalReport1()
            Dim dstRpt As New DataSet()
    Dim conDb As New SqlConnection("data source=localhost;initial catalog=databasename;user id=sa;password=sa")//根据实际不同
            conDb.Open()
            Dim dadReport As New SqlDataAdapter("select * from " + tableName + " ", condb)
            dadReport.Fill(dstRpt, tableName)
            oRpt.SetDataSource(dstRpt)
            cryRpt.ReportSource = oRpt
            conDb.Close()
        End Sub