0

显示水晶报告时出现此错误。我需要做什么。

建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)

我使用的代码如下:

protected void Page_Load(object sender, EventArgs e)
{        
    SqlConnection con = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=~/App_Data/Database.mdf;Integrated Security=True;User Instance=True");        
    DataSet1 ds = new DataSet1();
    SqlDataAdapter da = new SqlDataAdapter("SELECT        ID, Name, Dept, Salary FROM            dbo.Table2", con);
    da.Fill(ds.View2);
    ReportClass myReportObject = new ReportClass();
    myReportObject.ResourceName = "CrystalReport1.rpt";        
}
4

1 回答 1

0

我认为您尝试显示的 Crystal 报表是在另一个连接字符串下创建的,该连接字符串具有另一个服务器名称。将服务器名称和数据库名称分别传递给报告。该代码将有所帮助。

        ReportDocument cryRpt = new ReportDocument();
        TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
        TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
        ConnectionInfo crConnectionInfo = new ConnectionInfo();
        Tables CrTables ;

        cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt");

        crConnectionInfo.ServerName = "YOUR SERVER NAME";
        crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";

       // if sql server authentication mood

        crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
        crConnectionInfo.Password = "YOUR DATABASE PASSWORD";

        CrTables = cryRpt.Database.Tables ;
        foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
        {
            crtableLogoninfo = CrTable.LogOnInfo;
            crtableLogoninfo.ConnectionInfo = crConnectionInfo;
            CrTable.ApplyLogOnInfo(crtableLogoninfo);
        }

        crystalReportViewer1.ReportSource = cryRpt;
        crystalReportViewer1.Refresh(); 
于 2013-04-07T15:51:15.860 回答