Use SQL View or SQL Query?

前端 未结 9 1329
旧巷少年郎
旧巷少年郎 2021-01-17 22:46

I am working on an application to get data from a MS-SQL server (2005). In the command text, I can pass a sql query like this:

string query = \"SELECT T1.f1,         


        
9条回答
  •  星月不相逢
    2021-01-17 23:18

    In general I've found it's best to use views for several reasons:

    • Complex queries can get hard to read in code
    • You can change the view without recompiling
    • Related to that, you can handle changes in the underlying database structure in the view without it touching code as long as the same fields get returned

    There are probably more reasons, but at this point I would never write a query directly in code.

    You should also look into some kind of ORM (Object Relational Mapper) technology like LINQ to SQL (L2S). It lets you use SQL-like queries in your code but everything is abstracted through objects created in the L2S designer.

    (I'm actually moving our current L2S objects to run off of views, actually. It's a bit more complicated because the relationships don't come through as well... but it's allowing me to create one set of objects that run across 2 databases, and keep everything nicely abstracted so I can change the underlying table names to fix up naming conventions. )

提交回复
热议问题