VB.Net切换目标框架至.NET Framework 4.8后Crystal Report要求登录凭证
.NET Framework 4.8下Crystal Report弹出登录凭证问题的解决方案
项目目标框架为.NET Framework 4时,Crystal Report运行完全正常;切换至.NET Framework 4.8后,Crystal Report突然要求输入登录凭证,相关代码如下:
Dim cryRpt As New ReportDocument Dim connInfo As ConnectionInfo = New ConnectionInfo() connInfo.ServerName = tmpSN connInfo.DatabaseName = tmpDBN connInfo.UserID = tmpUID connInfo.Password = tmpPass Dim tableLogOnInfo As TableLogOnInfo = New TableLogOnInfo() tableLogOnInfo.ConnectionInfo = connInfo cryRpt.Load(reportsfolder + "NEWAPPFORM\AppForm.rpt") cryRpt.SetDataSource(tbIndividual) cryRpt.Subreports("Dependents.rpt").SetDataSource(cmdtableDep) For Each table As Table In cryRpt.Database.Tables table.ApplyLogOnInfo(tableLogOnInfo) table.LogOnInfo.ConnectionInfo.ServerName = connInfo.ServerName table.LogOnInfo.ConnectionInfo.DatabaseName = connInfo.DatabaseName table.LogOnInfo.ConnectionInfo.UserID = connInfo.UserID table.LogOnInfo.ConnectionInfo.Password = connInfo.Password Next .CrystalReportViewer1.ReportSource = cryRpt .CrystalReportViewer1.Refresh() .CrystalReportViewer1.ShowPrintButton = True .ShowDialog()
问题原因及解决方法
1. 遗漏子报表的登录信息设置
原代码仅处理了主报表的数据库表登录信息,但未处理子报表中的表。.NET 4.8下Crystal Reports对连接验证的逻辑更严格,子报表未配置登录信息会触发凭证输入提示。
修改代码,添加子报表的登录信息遍历设置:
' 处理主报表表登录信息 For Each table As Table In cryRpt.Database.Tables table.ApplyLogOnInfo(tableLogOnInfo) table.LogOnInfo.ConnectionInfo.ServerName = connInfo.ServerName table.LogOnInfo.ConnectionInfo.DatabaseName = connInfo.DatabaseName table.LogOnInfo.ConnectionInfo.UserID = connInfo.UserID table.LogOnInfo.ConnectionInfo.Password = connInfo.Password ' 强制指定表位置,覆盖报表中保存的旧连接信息 table.Location = $"{connInfo.DatabaseName}.{table.Location.Split(".").Last()}" Next ' 新增:处理所有子报表的表登录信息 For Each subRpt As ReportDocument In cryRpt.Subreports For Each subTable As Table In subRpt.Database.Tables subTable.ApplyLogOnInfo(tableLogOnInfo) subTable.LogOnInfo.ConnectionInfo.ServerName = connInfo.ServerName subTable.LogOnInfo.ConnectionInfo.DatabaseName = connInfo.DatabaseName subTable.LogOnInfo.ConnectionInfo.UserID = connInfo.UserID subTable.LogOnInfo.ConnectionInfo.Password = connInfo.Password subTable.Location = $"{connInfo.DatabaseName}.{subTable.Location.Split(".").Last()}" Next Next
2. 适配.NET 4.8的Crystal Reports运行时
确保安装了支持.NET Framework 4.8的Crystal Reports运行时版本,旧版本运行时与4.8兼容性较差,会导致连接验证异常。
3. 禁用登录提示
在绑定报表源后,添加以下代码关闭自动登录提示:
.CrystalReportViewer1.LogOnInfoPrompt = False
4. 清理编译缓存
删除项目的bin和obj目录,重新编译项目,避免旧版本的报表缓存或编译文件干扰连接逻辑。
5. 验证数据库权限
确认使用的数据库账号在.NET 4.8环境下具备访问目标数据库及表的权限,框架升级后安全策略可能限制了部分权限。
内容的提问来源于stack exchange,提问作者JeffyO
相关产品推荐
相关产品推荐

