C# Crystal Reports,为什么即使我提供了详细信息,它仍然要求登录数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11473407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Crystal reports, why is it asking for database login even after I provided the details?
提问by Razort4x
I am generating a report, but the problem is even though I've supplied credentials, when the form containing the CrystalReport opens up, it still asks me for them, and the worst part is, I don't enter any thing in there, and just click finish, and it loads the report. So, if there is no need for credentials (or whatever) why is it asking me?
我正在生成一份报告,但问题是即使我提供了凭据,当包含 CrystalReport 的表单打开时,它仍然要求我提供它们,最糟糕的是,我没有在其中输入任何内容,只需单击完成,它就会加载报告。那么,如果不需要凭据(或其他任何东西),它为什么要问我?
Here's the code
这是代码
private void MainReport_Load(object sender, EventArgs e)
{
var constr = string.Empty;
constr = Application.StartupPath;
if (Generate.bForProjects)
constr = Path.Combine(constr, @"Reports\Projects.rpt");
else
constr = Path.Combine(constr, @"Reports\Other.rpt");
var myConInfo = new CrystalDecisions.Shared.TableLogOnInfo();
reportDocument1.Load(constr);
myConInfo.ConnectionInfo.DatabaseName = "ProjectData.mdb";
myConInfo.ConnectionInfo.ServerName = Application.StartupPath + @"\Data\ProjectData.mdb";
myConInfo.ConnectionInfo.Password = "";
myConInfo.ConnectionInfo.UserID = "";
reportDocument1.Database.Tables[0].ApplyLogOnInfo(myConInfo);
reportDocument1.Refresh();
crystalReportViewer1.ReportSource = reportDocument1;
crystalReportViewer1.Width = this.Width - 50;
crystalReportViewer1.Height = this.Height - 100;
}
When the form loads, this screen pop ups
当表单加载时,此屏幕弹出


And, when this comes up, I don't enter anything! That's right! I just click finish and it loads the report perfectly! So, if it doesn't needs anything, why the hel* is it asking me for a login?
而且,当出现这种情况时,我什么都不输入!这是正确的!我只需单击完成即可完美加载报告!所以,如果它不需要任何东西,为什么它会要求我登录?
采纳答案by Adam Pridmore
We have lots of problems when we came to connect a crystal report to a different database to the one it was designed / created against. We ended up creating the following function to set it up correctly. It recursively sets the connection on all report tables and sub reports.
当我们将水晶报告连接到不同的数据库时,我们遇到了很多问题,因为它是针对它设计/创建的。我们最终创建了以下函数来正确设置它。它以递归方式在所有报告表和子报告上设置连接。
Also I seem to remember we had problems if the report was designed with Integrated Windows Authentications, and run with a simple username and password. So we always made sure we designed the reports with a simple username and password connection to the database.
此外,我似乎记得如果报告是使用集成的 Windows 身份验证设计的,并使用简单的用户名和密码运行,我们就会遇到问题。所以我们总是确保我们设计的报告使用简单的用户名和密码连接到数据库。
private static void SetConnection(ReportDocument report, string databaseName, string serverName, string userName, string password)
{
foreach (Table table in report.Database.Tables)
{
if (table.Name != "Command")
{
SetTableConnectionInfo(table, databaseName, serverName, userName, password);
}
}
foreach (ReportObject obj in report.ReportDefinition.ReportObjects)
{
if (obj.Kind != ReportObjectKind.SubreportObject)
{
return;
}
var subReport = (SubreportObject)obj;
var subReportDocument = report.OpenSubreport(subReport.SubreportName);
SetConnection(subReportDocument, databaseName, serverName, userName, password);
}
}
private static void SetTableConnectionInfo(Table table, string databaseName, string serverName, string userName, string password)
{
// Get the ConnectionInfo Object.
var logOnInfo = table.LogOnInfo;
var connectionInfo = logOnInfo.ConnectionInfo;
// Set the Connection parameters.
connectionInfo.DatabaseName = databaseName;
connectionInfo.ServerName = serverName;
connectionInfo.Password = password;
connectionInfo.UserID = userName;
table.ApplyLogOnInfo(logOnInfo);
if (!table.TestConnectivity())
{
throw new ApplicationException(Resource.UnableToConnectCrystalReportToDatabase);
}
table.Location = Database + "." + "dbo" + "." + table.Location;
}
回答by user2404603
Just create report object and add below link rptobj is crystalreport object and setdatabaselogin is method which take user id and password as a parameter.
只需创建报告对象并在下面的链接中添加 rptobj 是 Crystalreport 对象, setdatabaselogin 是将用户 ID 和密码作为参数的方法。
rptobj.setdatabaselogon(userid,password)
rptobj.setdatabaselogon(userid,password)
回答by Abid Quraishi
It only appears when your DataSet or DataTable is empty. So make sure it has data.
它仅在您的 DataSet 或 DataTable 为空时出现。所以确保它有数据。

