【问题标题】:Multiple stored procedures in VS 2010VS 2010 中的多个存储过程
【发布时间】:2015-02-03 09:15:35
【问题描述】:

我是在 Visual Studio 2010 中使用 C# 的新手,所以我的知识相当有限。 我正在寻找一个在 SQL Server 2008 中运行和导出多个存储过程的程序——也有一个很好的界面。 我不确定如何最好地做到这一点?

我在想我想要一个可能带有树视图和数据网格视图的表单,然后执行存储过程。这仅适用于一个查询 - 但我的问题是如何最好地使用多个查询?我不希望每个存储过程都有不同的数据网格视图(我有很多)。我希望能够在我的树视图中选择不同的存储过程,并让 datagridview 中的数据进行更改,而不必每次都运行存储过程。其中一些非常耗时(> 1 分钟)。所以我问我要问的是,如果我能以某种方式一次将所有数据加载到我的程序中? 我可以让一个数据集保存多个表 - 还是我必须为我的所有存储过程创建不同的数据集?

【问题讨论】:

  • 听起来您正在设计 SQL Server Management Studio Express :-)

标签: c# .net sql-server-2008 tsql


【解决方案1】:

您想要的不是一次将所有数据加载到程序中,而是缓存第一次运行查询的结果。这样一来,人们就不必等待所有内容加载完毕才能查看一个数据集。

您目前描述的场景是,当您单击树视图中的特定项目时,相关的 SQL 将运行并且结果集合绑定到您的 datagridview,从而清除任何以前的数据。

缓存的做法插入了一个get-if-I-don't-already-have-it层和一种存储方法。像这样的:

public class MyResultClass
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
}

private static readonly Dictionary<string, IEnumerable<MyResultClass>> 
    CachedResults = new Dictionary<string, IEnumerable<MyResultClass>>();

protected void OnTreeViewSelectionChanged(object sender, EventArgs e)
{
    // I'm not overly familar with the treeview off the top of my head,
    // so get selectedValue however you would normally.
    var selectedValue = ????;
    IEnumerable<MyResultClass> results;

    // Try to find the already cached results and if false, load them 
    if (!CachedResults.TryGet(selectedValue, out results))
    {
        // GetResults should use your current methodology for getting the results
        results = GetResults(selectedValue);
        CachedResults.Add(selectedValue, results);
    }

    myGridView.DataSource = results;
}

注意:如果这是一个 web 应用程序而不是一个 winforms 应用程序,您将需要使用 ConcurrentDictionary 而不是 Dictionary 来确保此模式线程安全。

【讨论】:

  • 作为菜鸟,我无法完全弄清楚IEnumerable&lt;Results&gt;&gt; 是什么。主要是“结果”部分。
  • 对不起,我应该更具描述性。结果是您的数据的类型。我已经更新了我的帖子。当您获取信息并直接绑定它时,它就是集合的任何内容。因此,如果它不是可枚举的而是字符串,您将缓存字符串。如果您正在检索 DataTable,请缓存 DataTable 而不是 IEnumerable,等等。
  • 哈哈,你让我在这里测试,必须弄清楚很多东西,但这真的很有帮助。现在一切都像魅力一样 - 谢谢;-)
猜你喜欢
  • 1970-01-01
  • 2015-05-29
  • 2013-07-23
  • 2012-07-12
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 2013-07-11
  • 2021-09-24
相关资源
最近更新 更多