我得到这个错误,而显示水晶报告。我需要做什么?
在建立到SQL Server的连接时发生与网络相关或特定实例的错误。未找到服务器或无法访问服务器。验证实例名是否正确,SQL Server是否配置为允许远程连接。(提供程序:Named Pipes提供程序,错误: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";
}
我认为您正在尝试显示的水晶报告已在另一个连接字符串下进行,该字符串具有另一个服务器名称。将服务器名称和数据库名称分别传递给报表。代码会有帮助。
ReportDocument cryRpt = new ReportDocument();
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables ;
cryRpt.Load("PUT CRYSTAL REPORT PATH HERECrystalReport1.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();