Best way of constructing dynamic sql queries in C#/.NET3.5?

后端 未结 12 2858
走了就别回头了
走了就别回头了 2021-02-15 11:25

A project I\'m working on at the moment involves refactoring a C# Com Object which serves as a database access layer to some Sql 2005 databases.

The author of the existe

相关标签:
12条回答
  • 2021-02-15 11:45

    There a kind of experimental try at a QueryBuilder class at http://www.blackbeltcoder.com/Articles/strings/a-sql-querybuilder-class. Might be worth a look.

    0 讨论(0)
  • 2021-02-15 11:48

    Unless executiontime is really important, I would consider refactoring the business logic that (so often) tends to find its way down to the datalayer and into gazillion-long stored procs. In terms of maintainabillity, editabillity and appendabillity I always try to (as the C# programmer I am) lift code up to the businesslayer.

    Trying to sort out someone elses 8000 line SQL Script is not my favorite task.

    :)

    //W

    0 讨论(0)
  • 2021-02-15 11:48

    Its worth considering if you can implement as a parameterised strored procedure and optimise it in the database rather than dynamically generating the SQL via LINQ or an ORM at runtime. Often this will perform better. I know its a bit old fashioned but sometimes its the most effective approach.

    0 讨论(0)
  • 2021-02-15 11:50

    LINQ is the way to go.

    0 讨论(0)
  • 2021-02-15 11:52

    Linq to SQL together with System.Linq.Dynamic brings some nice possibilities.

    I have posted a couple of sample code snippets here: http://blog.huagati.com/res/index.php/2008/06/23/application-architecture-part-2-data-access-layer-dynamic-linq

    ...and here: http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/717004553931?r=777003863931#777003863931

    0 讨论(0)
  • 2021-02-15 11:52

    I'm coming at this late and have no chance for an upvote, but there's a great solution that I haven't seen considered: A combination of procedure/function with linq-to-object. Or to-xml or to-datatable I suppose.

    I've been this in this exact situation, with a massive dynamically built query that was kindof an impressive achievement, but the complexity of which made for an upkeep nightmare. I had so many green comments to help the poor sap who had to come along later and understand it. I was in classic asp so I had few alternatives.

    What I have done since is a combination of function/procedure and linq. Often the total complexity is less than the complexity of trying to do it one place. Pass some of the your criteria to the UDF, which becomes much more manageable. This gives you a manageable and understandable result-set. Apply your remaining distinctions using linq.

    You can use the advantages of both:

    • Reduce the total records as much as possible on the server; get as many crazy joins taken care of on the server. Databases are good at this stuff.
    • Linq (to object etc.) isn't as powerful but is great at expressing complex criteria; so use it for various possible distinctions that add complexity to the code but that the db wouldn't be much better at handling. Operating on a reduced, normalized result set, linq can express complixity without much performance penalty.

    How to decide which criteria to handle in the db and which with linq? Use your judgement. If you can efficiently handle complex db queries, you can handle this. Part art, part science.

    0 讨论(0)
提交回复
热议问题