Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

c# - how to connect crystal reports to remote database

I am using Oracle database on server with 20.34.34.4 ip I want to build client server application with csharp but can l build crystal reports on client side that is connected to the server that contains the database and how, thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well,

Considering that your environment is: - Windows Server 2008 64 bit

  • C # 4

  • Oracle 11

  • Suposing Crystal 13

Initially you should do a few things:

  • You must install the Oracle Driver 64 if your OS is 64-bit, And Then install Oracle Driver 32 in the same home_oracle, this shouldnt be the home1. SAP works with just home1.

  • You must install the Oracle Client Administrator mode.

  • After That You must configure your ODBC driver to Oracle Home 1. Right now test if your ODBC connection is successful.
  • Your application shouldnt run in 32 bit mode.
  • If you installed Both versions of Oracles do not forget That You Must Have Both drivers installed on Home 1 and have the same TNSNAMES.ORA in Both instances. Also do not forget que must be installed in administrator mode.

Well to make a .Net web application to connect to Oracle using Crystal Reports, then you should ensure a few things:

  1. the CR version installed on the server matches the one you use on your application / website

  2. que the assemblies of the CR installed on the server are referenced Correctly on your web.config

  3. Set the application pool you of your application to operate under permission LOCAL SERVICE

  4. FULL TRUST mode is enabled on your operating folder

  5. CrystalReportViewer aspnet_client and folders are copied to your operating folder in the server.

Here is the download pages:

Oracle

SAP Crystal

Here is the source code:

web.config

<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=DATA_BASE_NAME;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD;pooling=false;" providerName="System.Data.OracleClient"/>
    </connectionStrings>
    <appSettings>
        <add key="db" value="DATA_BASE_NAME"/>
        <add key="connectionString" value="Data Source=DATA_BASE_NAME;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD;pooling=false;"/>
    </appSettings>
    <system.web>
        <compilation debug="true" defaultLanguage="c#" targetFramework="4.0">
            <assemblies>
                <add assembly="System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="CrystalDecisions.CrystalReports.Engine, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                <add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                <add assembly="CrystalDecisions.ReportSource, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                <add assembly="CrystalDecisions.Shared, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                <add assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
            </assemblies>
        </compilation>
    </system.web>
</configuration>

ReportByCrystal.aspx.cs

<%@ Page Language="C#" AutoEventWireup="true" Theme="theme" CodeFile="ReportByCrystal.aspx.cs"
    Inherits="ReportByCrystal" Title="Report By Crystal" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<head>

</head>
<body>
    <form id="ReportCrystal" runat="server">
        <div>
            <CR:CrystalReportViewer ID="crvCrystalReport" runat="server" AutoDataBind="true" />
        </div>
    </form>
</body>

ReportByCrystal.cs

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;

public partial class ReportByCrystal : Page
{
    //#region [ Page Events ]

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this.ReportParameter();
        }
        catch (Exception exc)
        {

        }

        if (!IsPostBack)
        {

        }
    }

    //#endregion

    //#region [ Methods ]

    /// <summary>
    /// 
    /// </summary>
    private void ReportParameter()
    {
        string reportName = "ReportName.rpt"
        string reportPath = Server.MapPath(String.Concat("~/reports/source/", reportName));
        string serverName = System.Configuration.ConfigurationManager.AppSettings["db"].ToString();
        string databaseName = String.Empty;
        string userId = Session["USERNAME"].ToString();
        string password = Session["PASSWORD"].ToString();

        if (File.Exists(reportPath))
        {
            ReportDocument reportDoc = new ReportDocument();
            reportDoc.Load(reportPath);

            foreach (ParameterField paramField in reportDoc.ParameterFields)
            {
                paramField.CurrentValues.Clear();
            }

            reportDoc.SetDatabaseLogon(userId, password, serverName, databaseName);

            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            crConnectionInfo.Type = ConnectionInfoType.SQL;
            crConnectionInfo.AllowCustomConnection = true;
            crConnectionInfo.IntegratedSecurity = false;
            crConnectionInfo.ServerName = serverName;
            crConnectionInfo.DatabaseName = databaseName;
            crConnectionInfo.UserID = userId;
            crConnectionInfo.Password = password;

            this.ApplyConnection(reportDoc, crConnectionInfo);

            this.crvCrystalReport.EnableParameterPrompt = true;
            this.crvCrystalReport.ReportSource = reportDoc;
            this.crvCrystalReport.RefreshReport();
        }
        else
        {
            this.ltrTitulo.Text = String.Concat("Report not found.");
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="report"></param>
    /// <param name="connectionInfo"></param>
    private void ApplyConnection(ReportDocument report, ConnectionInfo connectionInfo)
    {
        this.ApplyLogOnInfo(report, connectionInfo);
        this.ApplyLogOnInfoForSubreports(report, connectionInfo);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="reportDocument"></param>
    /// <param name="connectionInfo"></param>
    private void ApplyLogOnInfo(ReportDocument reportDocument, ConnectionInfo connectionInfo)
    {
        foreach (CrystalDecisions.CrystalReports.Engine.Table tableTemp in reportDocument.Database.Tables)
        {
            if (tableTemp.Name.ToUpper().StartsWith("COMMAND"))
            {

            }

            TableLogOnInfo tableLogonInfo = tableTemp.LogOnInfo;
            tableLogonInfo.ConnectionInfo = connectionInfo;
            tableTemp.ApplyLogOnInfo(tableLogonInfo);

            bool b = tableTemp.TestConnectivity();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="reportDocument"></param>
    /// <param name="connectionInfo"></param>
    private void ApplyLogOnInfoForSubreports(ReportDocument reportDocument, ConnectionInfo connectionInfo)
    {
        foreach (Section sectionTemp in reportDocument.ReportDefinition.Sections)
        {
            foreach (ReportObject reportObjectTemp in sectionTemp.ReportObjects)
            {
                if (reportObjectTemp.Kind == ReportObjectKind.SubreportObject)
                {
                    SubreportObject subreportObject = (SubreportObject)reportObjectTemp;

                    ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);

                    this.ApplyLogOnInfo(subReportDocument, connectionInfo);
                }
            }
        }
    }
}

Regards,

Andrew Paes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...