简体   繁体   English

在 ADO.NET(包装类)中编写更少的代码

[英]Writing less code in ADO.NET (wrapper classes)

So I'm pretty new to C# in general, but I have a basic knowledge of the language at the moment.所以总的来说,我对 C# 还是很陌生,但我目前对该语言有基本的了解。

When accessing an SQL database, I've decided that using ADO.NET (SqlClient seems to be the best way to do it, and I have managed to get it working, including using queries. My issue is that as soon as I start to query the database more often, I seem to just be rewriting very similar code over and over again (which is obviously bad practice). It's clear that I need to make some sort of class that handles my use of ADO.NET (SqlClient) so that I cant just type something like the following:在访问 SQL 数据库时,我决定使用 ADO.NET (SqlClient 似乎是最好的方法,我已经设法让它工作,包括使用查询。我的问题是,一旦我开始查询数据库更频繁,我似乎只是一遍又一遍地重写非常相似的代码(这显然是不好的做法).很明显,我需要制作某种 class 来处理我对 ADO.NET (SqlClient) 的使用所以我不能只是键入如下内容:

DatabaseConnection.Query("SELECT * FROM tblProducts");

… instead of... … 代替...

using (SqlConnection cnn = new SqlConnection(cnnString)) // cnnString was defined earlier in the code
{
    SqlCommand cmd = new SqlCommand("SELECT ProductType FROM tblProductTypes", cnn);
    DataTable dt = new DataTable();
    cnn.Open();
    dt.Load(cmd.ExecuteReader());
    cboFilterTypes.DataSource = dt;
    cboFilterTypes.DisplayMember = "ProductType";
    cboFilterTypes.ValueMember = "ProductType";
    cnn.Close();
}

So I think I need to make a class wrapper (at least that's what I believe it is called), but I'm not too sure on how to go about doing it.所以我认为我需要制作一个 class 包装器(至少我认为它是这样称呼的),但我不太清楚如何 go 来做这件事。 Does anyone have any suggestions or tricks that I can use?有没有人有任何我可以使用的建议或技巧?

To be clear, I am not wanting to use the entity framework or anything like that which is built on ADO.NET - I've tried to entity framework and have decided there are too many drawbacks.需要明确的是,我不想使用实体框架或基于 ADO.NET 构建的类似框架 - 我尝试过实体框架并认为存在太多缺点。

Sticking with your request for a 'wrapper' you could simply make a static helper method to handle the call to the database such as坚持您对“包装器”的要求,您可以简单地制作一个 static 辅助方法来处理对数据库的调用,例如

    public static DataTable GetDataTable(string query)
    {
        DataTable result = new DataTable();
    using (SqlConnection cnn = new SqlConnection(cnnString)) // cnnString was defined earlier in the code
    {
            SqlCommand cmd = new SqlCommand(query, cnn);
            result = new DataTable();
            cnn.Open();
            result.Load(cmd.ExecuteReader());
            cnn.Close();
     }
return result;
    }

You could then call this like然后你可以这样称呼

    cboFilterTypes.DataSource = GetDataTable("SELECT ProductType FROM tblProductTypes")
    cboFilterTypes.DisplayMember = "ProductType";
    cboFilterTypes.ValueMember = "ProductType";

I wouldn't recommend this way of data access for anythig other than a tiny project.除了小型项目之外,我不会推荐这种数据访问方式。 If you really don't like EF then Dapper or something similar offers a much more scalable solution.如果你真的不喜欢 EF,那么 Dapper 或类似的东西提供了一个更具可扩展性的解决方案。 Stackoverflow itself uses Dapper:) Stackoverflow 本身使用 Dapper :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM