English 中文(简体)
什么是。 NET 创建通用服务器系统的方法?
原标题:What are some .NET methods for creating a generic SQL Server query?

我只想在C# .NET 4.0中写出一种通用功能,在那里,我可以寄送一个数据库,显示情况,并重述我的成果。

由于没有在C#中广为人知,而且它带有不同的物体,我并不真正确定返还这一信息的最佳选择是什么。

我知道有<代码>可测量,>DataSet,而且确实有这一条。 我所期待的是一个目标,效率相当高,我可以轻易获得数据。 我确信,这不会是我自己撰写的一个问题,但我知道,在4.NET中,我可以接触到一些其他物体。

最佳回答

The problem with such a design is that it s very difficult to use best practices for security and preventing SQL Injection. Passing in a pure SQL statement to a function pretty much eliminates the ability to defend against SQL Injection unless you do it carefully.

作为一般规则,如果您在 任何上重新计算,请将其重新计算。 用户投入,请您确保您要么使用,要么储存程序,要么

我们使用Microsoft Data Application/2005/4, 摘自其模式和做法图书馆,该图书馆含有使这一功能真正变得更容易得多的功能。

在使用这一系统时,联线储存在座标或网上。

我们的法典在利用这一模式时也这样做:

public static DataSet ExecuteDataset(string CallingApp, string ConnectionStringName, string StoredProcName, object[] ParameterValues)
{
        DataSet ReturnValue = new DataSet();

        Microsoft.Practices.EnterpriseLibrary.Data.Database db = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(ConnectionStringName);
        try
        {
            ReturnValue = db.ExecuteDataSet(StoredProcName, ParameterValues);
        }
        catch (Exception ex)
        {
            // a bunch of code for exception handling removed            
        }


        return ReturnValue;
 }

这是获得数据单的法典。 还有其他许多职能,而这一守则可能不利于最新的数据检索图书馆。 我们再使用略为老的版本,但这应当使大家了解如何容易使用。

问题回答

New and improved method is not to do that, but to havea method to get the data you want.

E.g 界定了<代码>CustomerOrder 班级。 有一个数据库模块,其功能称为:

GetOpenOrdersForCustomer(int argCustomerID) 

谨请将此信收复。

Look up Entity Framework POCO etc.

<代码>DataSet和DataTable是此类物品的早期吸入器,其脂重,重重重重行李。

If you want/need to do a bit more handcoding. Interfaces like IDataReader, IDbCommand IDbConnection, will isolate you from a number of implementation details in your code, for instance whether your backend is SQL Server, MySql etc.

即便如此,您在应用守则中仍然最好不要通过诸如<条码>的可发射<>代码>、<条码>、SqlCommand等内容。

Too many chances to to plaster the database schema all over the code leaving you with a huge and soon to be realised potential for technical debt.

.。 在《刑法》第一版中,实际上看不见。

It depends the scenario you want to cover. For example, in an MVC application its fairly maintainable to deploy a data layer based on Entity Framework, because its easily testeable and the communication with the database is very straightforward through simple objects.

另一种做法是使用已经开发的部分,例如企业图书馆的数据应用区,或或许开发你的海关数据库工厂。 这完全取决于你们的发展目的。

您能否更详细地介绍我们所谈论的哪类应用? 它拥有现有的数据库或新的数据库?

另一个问题。 你们完全希望通过提问? 如果你听从轻判,我会告诉你,你应当避免的非常坏和危险的做法。

最佳方面。

你们可能想要的是:

public static DbDataReader Query(
    string connectionString, string selectCommand, 
    params KeyValuePair<string, object>[] parameters)
{
    var connection = new OleDbConnection(connectionString);
    var command = new OleDbCommand(selectCommand, connection);

    foreach (var p in parameters)
        command.Parameters.Add(new OleDbParameter(p.Key, p.Value));

    var result = command.ExecuteReader();
    return result;
}

OleDb被用作例子。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签