我有一个连接到Access数据库(.mdb)的简单应用程序。我只使用代码将数据库文件连接到应用程序。连接字符串如下,
“Provider = Microsoft.Jet.OLEDB.4.0; Data Source = | DataDirectory | \ CentreDB.mdb; Jet OLEDB:Database Password = pass”
出于设计目的,我使用报告向导中的“连接数据库文件”选项将数据库文件连接到水晶报告。但是我需要使用代码动态地将数据库分配给crystal报表,以便在我在客户端计算机中部署它时不会遇到问题。我尝试使用下面的代码。
public void ViewReport(FileInfo crFileInfo) // path\filename.rpt
{
ConnectionInfo crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = "YOUR SERVER NAME";
crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";
crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
crConnectionInfo.Password = "YOUR DATABASE PASSWORD";
// CrystalReportViewer control
crViewer.ReportSource = ReportSourceSetup(crFileInfo, crConnectionInfo);
crViewer.Refresh();
}
ReportDocument ReportSourceSetup(FileInfo crFileInfo, ConnectionInfo crConnectionInfo)
{
ReportDocument crDoc = new ReportDocument();
TableLogOnInfos crTableLogonInfos = new TableLogOnInfos();
TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
Tables crTables;
crDoc.Load(crFileInfo.FullName);
// Each table in report needs to have logoninfo setup:
crTables = crDoc.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crTableLogonInfo = crTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogonInfo);
}
return crDoc;
}
我在网上发现了这个样本,但我无法让它发挥作用。我需要知道我需要在哪里放置这个& amp;我应该传递“你的服务器名称”,“你的数据库名称”,因为我使用的是只有密码的数据库文件。我认为FileInfo对象携带数据库的路径。请尽可能帮助我。任何帮助将不胜感激。
答案 0 :(得分:0)
通过访问我相信你应该指定一个DSN作为服务器名,所以在机器上设置一个DSN并以这种方式引用它...如果你命名为dsn access_dsn:
ConnectionInfo crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = "access_dsn"; //dsn name
crConnectionInfo.DatabaseName = ""; //leave blank, it knows from DSN
crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
crConnectionInfo.Password = "YOUR DATABASE PASSWORD";