簡體   English   中英

水晶報表——關閉數據庫連接

[英]Crystal reports - close the database connection

這是C#,Visual Studio 2008,VS2008自帶的水晶報表

我有一個位於 DLL 中的水晶報表查看器表單。DLL 負責加載水晶報表(基於報表文件名),並在表單上顯示報表。

當我完成水晶報表時,我在加載的報表文檔 object 上調用處置。但是,數據庫連接仍然存在。

Crystal 似乎檢測到有其他連接(從我的主應用程序)到同一數據庫,並保持其連接打開。 當主應用程序數據庫連接關閉時,水晶連接也關閉。

有沒有辦法強制 crystal 關閉它的連接,而不關閉主應用程序數據庫連接?

如何通過設置身份驗證在運行時創建自己的連接,或者讓Crystal通過報告中存儲的連接進行連接,如何連接到數據庫? 如果您以任何方式,形狀或形式進行自己的連接,則必須手動關閉連接並在處理報告之前調用dispose。

這很可能是內存泄漏。 我以前經歷過這些。 Crystal Reports也存在內存泄漏問題,並且在他們的論壇上談了很多但幾年前我使用它時沒有發布修復程序。 我為其他選擇拋棄了水晶。

標記代碼似乎在某種程度上緩解了這種情況,雖然它有點倒退,應該是這樣的:

ReportDocument rd = (ReportDocument) viewer.ReportSource;
foreach (Table table in rd.Database.Tables)
    table.Dispose();
viewer.ReportSource = null;
rd.Database.Dispose();
rd.Close();
rd.Dispose();
rd = (ReportDocument) viewer.ReportSource;  
GC.Collect();

這對我來說並沒有完全堵塞泄漏,但肯定有幫助。

connectDB();

ReportDocument cryRpt = new ReportDocument();
DataSet ds;
cmd = new SqlCommand("Select * from LeaveJO where IDno = '" + textBox1.Text + "'", conn);

da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds, "LeaveJO");
            cryRpt.Load(@"JOleave.rpt");
            cryRpt.SetDataSource(ds);

            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables;

crConnectionInfo.ServerName = cs.serverName;
crConnectionInfo.DatabaseName = cs.dbName;
crConnectionInfo.UserID = cs.userID;
crConnectionInfo.Password = cs.password;

CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
    crtableLogoninfo = CrTable.LogOnInfo;
    crtableLogoninfo.ConnectionInfo = crConnectionInfo;
    CrTable.ApplyLogOnInfo(crtableLogoninfo);
}

cryRpt.Refresh();
//  cryRpt.PrintToPrinter(2, true, 1, 2);
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Visible = true;
cryRpt.Dispose();
conn.Dispose();

您可以更改 CRConfig.xml 中的超時選項,以便自動關閉連接。

我不太熟悉Crystal Reports,但由於包含IDisposable接口的繼承鏈,有許多對象具有無用的Dispose()方法。 如果您沒有在服務器上看到任何性能問題,請不要擔心。 GC准備就緒后,將負責客戶端上的連接。 你不應該試圖比GC更聰明,你只會給自己帶來更多的麻煩。

並且,當它可用時,始終調用Dispose()(或使用{})。

除了我正在使用Sybase之外,我遇到了同樣的問題。 不久之前,當我看到錯誤時,我發布了我編寫的代碼來處理連接(並且沒有用)! 我修正了錯誤,並且,交叉我的手指,現在它似乎工作。 我打開了近100份報告,之前我無法打開10個。 如果你試試這個,請告訴我它是否適合你。

在關閉包含Crystal Reports查看器的窗口之前,我正在做的就是:

var rd = (ReportDocument)crystalReportViewer1.ReportSource;
foreach (Table table in rd.Database.Tables)
   table.Dispose();
rd.Database.Dispose();
rd.Close();
rd.Dispose();
GC.Collect();

標記

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM