Crystal Report 13中的动态数据库连接...? [英] Dynamic Database Connection in Crystal Report 13...?

查看:53
本文介绍了Crystal Report 13中的动态数据库连接...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET(C#)项目,并且我正在使用Crystal Report生成报告..
为了报告,我正在使用MS Access数据库

现在我的问题是,当我的项目当时在客户端PC上运行时,我的报表将使用客户端PC上保存的数据库,而在我的PC上何时意味着在开发时该报表将使用我的数据库...

两个数据库也具有相同的架构和数据..


意味着我要动态连接数据库...

在客户端计算机上,它将使用客户端数据库,而在我的计算机上,它将使用我的数据库...

我想在运行时设置数据库信息....

I have one ASP.NET (C#) Project and i am using Crystal Report for generating reports..
and for report i am using MS Access Database

Now my question is that when my project runs on client pc at that time my report will use the database which is held on client pc and when it will in my pc means at development that time report use my database...

Both database have same schema and data as well..


Means i want the database connection dynamically...

at client pc it will use client database and in my pc it will use my database...

I want to set database information at runtime....

推荐答案

在这里,我找到了上述针对我的问题的解决方案...

在这里,我声明了一个函数,它将在运行时设置我的数据库信息....

这里setDbInfo是用于设置db info的函数,此函数将在将报告放置到报告文档之后,在插入到报告查看器之前被调用,如DisplayReport函数中所示...

Here i find this solution for My Question mentioned as above...

Here i have declare one Function that will set my database information at run time....

here setDbInfo is the function for seting db info this function will called after loding report to report document and before assining to report viewer like shown in DisplayReport function...

protected void setDbInfo(ReportDocument rpt, string ServerName, string DatabaseName, string UserName, string Password)
        {
            TableLogOnInfo logoninfo = new TableLogOnInfo();
            foreach (CrystalDecisions.CrystalReports.Engine.Table t in rpt.Database.Tables)
            {
                logoninfo = t.LogOnInfo;
                logoninfo.ReportName = rpt.Name;
                logoninfo.ConnectionInfo.ServerName = ServerName;
                logoninfo.ConnectionInfo.DatabaseName = DatabaseName;
                logoninfo.ConnectionInfo.UserID = UserName;
                logoninfo.ConnectionInfo.Password = Password;
                logoninfo.TableName = t.Name;
                t.ApplyLogOnInfo(logoninfo);
                t.Location = t.Name;
            }
        }

        protected void DisplayReport()
        {
            try
            {
                ReportDocument rpt = new ReportDocument();
                rpt.Load(Server.MapPath("~/CRList.rpt")); 
                string ServerName = ConfigurationManager.AppSettings["Server"].ToString();
                string DatabaseName = ConfigurationManager.AppSettings["DBName"].ToString();
                string UserName = ConfigurationManager.AppSettings["UserName"].ToString();
                string Password = ConfigurationManager.AppSettings["Password"].ToString();
                setDbInfo(rpt,ServerName,DatabaseName,UserName,Password);
                CRPTViewer.ReportSource = rpt;
                CRPTViewer.RefreshReport();
            }
            catch (Exception exc)
            {   
                throw exc;
            }
        }


1.创建一个ReportDocument对象,说对象名称:objRpt.
objReport = new yourreportname();

2.编写以下代码:

1.Create an object of ReportDocument say object name : objRpt.
objReport = new yourreportname();

2. Write following code :

foreach (Table crTable in objRpt.Database.Tables)
            {
                TableLogOnInfo logOnInfo = new TableLogOnInfo();
                logOnInfo = objRpt.Database.Tables[crTable.Name].LogOnInfo;

                // Set the connection information for the table in the report.
                logOnInfo.ConnectionInfo.ServerName = server.Trim();
                logOnInfo.ConnectionInfo.DatabaseName = database.Trim();
                logOnInfo.ConnectionInfo.UserID = username;
                logOnInfo.ConnectionInfo.Password = password;
                objRpt.Database.Tables[crTable.Name].ApplyLogOnInfo(logOnInfo);


}
希望您可以从config
获取连接详细信息
3.创建一个CrystalReportViewer类的对象,说对象名称:objCrystalReportViewer
.

4. objCrystalReportViewer.ReportSource = objRpt;
5. objCrystalReportViewer.Show()


}
I hope you can get connection details from config

3. Create an object of CrystalReportViewer class say object name : objCrystalReportViewer
.

4. objCrystalReportViewer.ReportSource = objRpt;
5. objCrystalReportViewer.Show()


亲爱的朋友

正如您所说,这是不可能的,要实现这一目标
不要将ur Crystal报表直接与数据库连接
在您的项目中添加数据集并在该数据集中添加数据表,现在在该项目中设计ur字段
数据表并将ur crystal报告与该数据表连接,现在可以动态填充该数据表
通过使用这种方法,U可以使您的报告充满活力
Dear friend

this is not possible as U r Saying, to achive this
Don''t connect ur Crystal report directly with database
Add a dataset in ur project and add datatable in that dataset, now design ur fields in that
datatable and connect ur crystal report with that datatable, now dynamicly fill that datatable
by using this method U can make ur report dyanamic


这篇关于Crystal Report 13中的动态数据库连接...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆