将经常重复的代码放在函数中?

发布于 2024-10-08 17:33:47 字数 889 浏览 0 评论 0原文

我经常在 OnClicks 和 OnLoads 中使用此代码或此代码的某种形式:

 DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("administratorGetAll", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }

所以我想知道最小化如此大的重复的最佳方法是什么;功能?

我目前正在尝试这样的事情:

 public void SqlStructGetAll(string storedProc, string connection)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(connection))
    {
        using (SqlCommand cmd = new SqlCommand(storedProc, con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }
}

但我不确定我是否离此还远。

I often use this code or a form of this code throughout OnClicks and OnLoads:

 DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("administratorGetAll", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }

So I was wondering what the best way to minimize such large duplications; function?

I am currently trying something like this:

 public void SqlStructGetAll(string storedProc, string connection)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(connection))
    {
        using (SqlCommand cmd = new SqlCommand(storedProc, con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }
}

But I am not sure if I am way off with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煞人兵器 2024-10-15 17:33:47

有一些设计模式可以解决此类设计问题,其中代码重复是主要问题。

适用于您的场景的模式是 Gang Of Four 模式目录中的模板方法设计模式。

您的方向是正确的 - 请参阅 http://www.dofactory.com/Patterns/PatternTemplate .aspx 了解设计模式的详细信息。

大多数现代框架都为数据库处理等操作提供实用程序包装器和模板方法 - 您可以使用提供此类方法的 Spring.NET。您可以查看此链接 http://www.springframework .net/docs/1.3.0/reference/html/ado.html 例如在 Spring .NET 中使用 ADO.NET API 的模板方法

There are design patterns that solve such design issues where code duplication is major concern.

The pattern applicable to your scenario is template method design pattern from Gang Of Four pattern catalog.

You are in the right direction - please refer to http://www.dofactory.com/Patterns/PatternTemplate.aspx for details on the design pattern.

Most modern day frameworks provide utility wrappers and template methods for operations like database handling - you can use Spring.NET that provides such methods. You can have a look at this link http://www.springframework.net/docs/1.3.0/reference/html/ado.html for example of template method to use ADO.NET API in Spring .NET

千笙结 2024-10-15 17:33:47

通过将数据访问放置在 onclick 和 onload 函数的代码隐藏中,您可以将数据访问层与表示层紧密耦合。虽然您认为应该从每个函数中提取这种类型的代码,但您的想法是正确的,但重新考虑您的架构可能会更好。 Dan Wahlin 有一个构建 n 层应用程序的好示例,它使用ADO.Net。您可能会从他的代码示例中得到一些好主意,这可能会对您将来有所帮助。

创建 N 层 ASP.NET 应用程序 - 视频

直接代码下载

By placing your data access in the codebehind for your onclick and onload functions you're tightly coupling your data access layer with your presentation layer. While you're on the right track in thinking that you should pull that type of code out of each function, you might be better of by rethinking your architecture. Dan Wahlin has a good example of building an n-layer application which uses ADO.Net. You might get some good ideas from his code example which may help you in the future.

Creating an N-Layer ASP.NET Application - Video

Direct Code download

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文