Skip to main content
 首页 » 编程设计

c#之Crystal 报表询问用户名和密码

2024年02月20日11Renyi-Fan

每次我打开包含 Crystal 报表的表单时,它总是要求输入用户名和密码,但我什至没有在我的网络应用程序中使用用户名和密码。我在我的 web.config 中使用集成安全性。我该如何解决这个问题?

这是代码:

            using System; 
            using System.Collections.Generic; 
            using System.Linq; 
            using System.Web; 
            using System.Web.UI; 
            using System.Web.UI.WebControls; 
            // FOR CRYSTAL REPORT 
            using CrystalDecisions.CrystalReports.Engine; 
            using CrystalDecisions.Shared; 
            using System.Data; 
            using System.Configuration; 
 
            // FOR SQL CONNECTION 
            using System.Data.Sql; 
            using System.Data.SqlClient; 
            using MediCard_Cooperative.App_Data; 
 
            namespace MediCard_Cooperative.MediCard_Cooperative.Reports 
            { 
                public partial class rptTest : System.Web.UI.Page 
                { 
                    private SqlConnection connSQL; 
 
                    protected void Page_Load(object sender, EventArgs e) 
                    { 
                        ReportDocument rptDoc = new ReportDocument(); 
                        dsMembers ds = new dsMembers();                     // .xsd or dataset filename 
                        DataTable dt = new DataTable(); 
 
                        // Set the name of data table 
                        dt.TableName = "Crystal Report Members"; 
                        dt = getAllData();                                  // calling 'getAllMembers' function 
                        ds.Tables[0].Merge(dt); 
 
                        // .rpt file path "../Reports/SimpleReports.rpt" 
                        rptDoc.Load(Server.MapPath("../Reports/CrystalReports/ctrSample.rpt")); 
 
                        //set dataset to the report viewer 
                        rptDoc.SetDataSource(ds); 
                        ctrViewerTest.ReportSource = rptDoc; 
                    } 
 
                    public DataTable getAllData() 
                    { 
                        try 
                        { 
                            using (connSQL = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connstring"].ToString())) 
                            { 
                                using (SqlCommand cmd = new SqlCommand("usp_Test", connSQL)) 
                                { 
                                    cmd.CommandType = CommandType.StoredProcedure; 
 
                                    connSQL.Open(); 
                                    cmd.ExecuteNonQuery(); 
 
                                    using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 
                                    { 
                                        DataSet ds = new DataSet(); 
 
                                        da.Fill(ds, "dtTest"); 
 
                                        return ds.Tables[0]; 
                                    } 
 
                                } 
 
                            } 
 
                        } 
                        catch (Exception ex) 
                        { 
                            throw new Exception(ex.Message); 
                        } 
                    } 
                } 
            } 

请您参考如下方法:

您必须通过 ReportDocument 对象提及数据库用户名和密码。

rptDoc.SetDatabaseLogon(username, password);