如何在WinForms之间使用GLOBAL数据集? [英] How to use GLOBAL Dataset Between WinForms?

查看:72
本文介绍了如何在WinForms之间使用GLOBAL数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个MainForm,一个MyClass和不同的DialogForms.我想使用一个数据集来存储我的组合框表,只要我的应用程序运行,因为每次打开dilaog表单时,我都不想进行任何查询来填充组合框.组合框在MyClass上定义,并在MainForm时填充.那么,哪种方法最适合我的情况?使用静态参数或某些智能建议等.预先感谢...:confused:

Hi all,
I have a MainForm, a MyClass and different DialogForms. I want to use a dataset for storing my combobox tables as long as my application runs, because every time opening a dilaog form I do not want to make any query to fill comboboxes. The combobox defined on MyClass and filled when MainForm. So, what is the best method for my situation? Using static parameter, or some smart suggetsions, etc. Thanks in advance...:confused:

推荐答案

创建一个静态的Globals类,该类包含应用程序范围的数据成员.

Create a static Globals class that holds app-wide data members.

public static class Globals
{
    public static DataSet MyData { get; set; }

    static Globals()
    {
        // initialize MyData property in the constructor with static methods
    }
}



这就是我这样做的方式.我知道-全局数据是邪恶的",但这是解决我想从任何地方都可以访问"要求的便捷解决方案.



This is the way I do it. I know - global data is "evil", but this is a handy solution to the "I want to get to it from anywhere" requirement.


您在这里真正想做的就是缓存.

应用程序启动时,需要从数据库或用于填充缓存的任何存储中获取一些数据.

我在这里使用的逻辑是这样的...

缓存逻辑图 [ CacheManager的PasteBin源代码 [ ^ ]

然后,您可以像下面这样致电...

What you really want to do here is caching.

When you application launches, it''s going to need to get some data from a database or whatever storage you are using to populate your cache.

The logic I use here is like this...

Cache Logic Diagram[^]

So, the first time you call a method, it will invoke the database operation and cache the data. Next time you call the method, the data is retrieved from the Cache.

You can implement this using a CacheManager class, source as follows

NB: this using the HttpRuntime caching mechanism, so you''d need a reference to System.Web in your project

PasteBin source code for CacheManager[^]

You can then call like follows...

/// <summary>
/// Execution delegate that returns some data
/// </summary>
/// <typeparam name="TValue">value</typeparam>
/// <returns>Business set</returns>
public delegate TValue DataRetrievalDelegate<TValue>();

public System.Data.DataTable GetCityData()
{
    return CacheManager.RetrieveFromCacheOrInvoke<System.Data.DataTable>("Cache:CityData",
       new DataRetrievalDelegate<System.Data.DataTable>(delegate()
       {
           // Your code here for retrieving data from some source!
           return new System.Data.DataTable();
       }));
}




如果您为此感到困扰,我会整理一个演示项目.

从缓存中检索或调用演示 [




I''ve put together a little demo project if you''re struggling with this.

Retrieve From Cache Or Invoke Demo[^]

Have a look in the Program.cs file. Put a breakpoint on the first line...run the project through line by line.

Notice how the first time it will call the delegate method, the second time it will retrieve from the cache

:thumbsup:


如果应用程序所有部分的数据都相同,那么将其作为静态属性保存在MainForm中确实是有意义的,因为那是首先加载的东西:它可以设置其Load事件中的值,以供其他所有东西使用.
If the data it going to be the same for all parts of your application, then it does make some sense to hold it as a static property, probably in your MainForm, since that is the first thing loaded: it can set the values in it''s Load event for everything else to use.


这篇关于如何在WinForms之间使用GLOBAL数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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