DataTable设置为DataSourceShare时报表需要登录数据库报表设计器在设计器中创建水晶报表,使用ODBC(DSN)连接直接连接数据库。通过DSN通过Winform(C#)应用程序执行相同的报告,并提供数据库服务器、数据库、用户ID和密码。我需要对CrystalReport对象进行此类更改。ReportDocument不应通过DSN直接连接到数据库。相反,我们将通过调用相应的存储过程来通过服务提供数据,参数为System.Data.DataTable。此DataTable对象应用于填充/生成报告。我分别从ReportDocument.DataBase.Tables[I].Location和ReportDocument.DataDefinition对象获取存储过程和参数信息。使用ReportDocument.DataBase.Tables[I].SetDataSource(DataTable)设置DataSource后,它仍然要求提供数据库/服务器和用户凭据以连接到服务器。我们能否实现该方案并使用内存表而不是通过ODBC直接连接到数据库来填充报告?您需要关注两件事:您正在连接到用于设计报表的同一服务器和数据库名称。您正在连接到另一个数据库或服务器。场景1:相同的服务器和数据库名称在这种情况下,您需要使用SetDatabaseLogon方法提供凭据,如下所示'crDoc1是您的ReportDocument'dtDataTable是您的DataTable'设置数据库登录信息crDoc1.SetDatabaseLogon("db_user_name","db_password","db_server_name_or_ip"“,“数据库名称”);'将DataTable设置为DataSourcecrDoc1.SetDataSource(dtDataTable)场景2:不同的服务器或数据库名称在这种情况下,您需要使用ApplyLogOnInfo方法提供凭据ConnectionInfocrConInfo=newConnectionInfo();TableLogOnInfocrTblLogonInfo=newTableLogOnInfo();crConInfo.ServerName="db_server_name_or_ip";crConInfo.DatabaseName="数据库名称";crConInfo.UserID="db_user_name";crConInfo.Password="db_password";'crDoc1是您的ReportDocumentTable'是您的DataTable'将DataSource设置为您的DataTablecrDoc1.SetDataSource(dtDataTable)'在设置DataSource后将登录凭据应用于ReportDocument表中的每个表CrTables=crDoc1.Database.Tables;foreach(CrTables中的CrystalDecisions.CrystalReports.Engine.TableCrTable){crTblLogonInfo=CrTable.LogOnInfo;crTblLogonInfo.ConnectionInfo=crConInfo;CrTable.ApplyLogOnInfo(crTblLogonInfo);}crDoc1.Refresh();CrystalReportViewer1.ReportSource=crDoc1;ApplyLogOnInfo应用于所有子报表及其表更新:ApplyingApplyLogOnInfotosubreports以上是C#学习教程:当设置DataTable为DataSource时,报表需要登录数据库分享所有内容,如果对大家有用需要了解more关于C#学习教程,希望大家多多关注foreach(ReportDocumentsrSubReportincrDoc1.Subreports){foreach(CrystalDecisions.CrystalReports.Engine.TableCrTableinsrSubReport.Database.Tables){crTblLogonInfo=CrTable.LogOnInfo;crTblLogonInfo.ConnectionInfo=crConInfo;CrTable.ApplyLogOnInfo(crTblLogonInfo);}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
