What is the best practice for writing sql queries inside c# code

雨燕双飞 提交于 2019-12-09 17:17:41

问题


On my current project I'm using SQL CE. Since it doesn't have support for stored procedures I have to write sql queries inside repository.

Option 1:

StringBuilder query = new StringBuilder();
query.Append("SELECT");
query.Append("    c.CUSTOMER_ID,");
query.Append("    COALESCE (c.FIRST_NAME, '') + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME");
query.Append("    ct.NAME as CUSTOMER_TYPE");
query.Append("FROM ");
query.Append("    CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID");

Option 2:

string query = "SELECT c.CUSTOMER_ID, COALESCE (c.FIRST_NAME, '') + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME, ct.NAME as CUSTOMER_TYPE FROM CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID";

Option 1 seems like a much more readable, especially when I have 10+ tables in join, but option 2 is faster.
Which option should I accept and what's the best practice in this case?


回答1:


Option 2 may be a few nanoseconds faster, but when you add the time to actually execute in the database (several milliseconds) a few extra nanaoseconds barely registers as noise.

In any case, there is another option that's the best of both worlds: @-strings:

string query = @"
    SELECT
        c.CUSTOMER_ID,
        COALESCE (c.FIRST_NAME, ''_ + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME
        ct.NAME as CUSTOMER_TYPE
    FROM
        CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c
            ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID
    ";



回答2:


Option 3 - use verbatim string literals:

string query = @"
SELECT 
    c.CUSTOMER_ID,
    COALESCE (c.FIRST_NAME, '') + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME,
    ct.NAME as CUSTOMER_TYPE
FROM 
    CT_CUSTOMER_TYPE AS ct 
  INNER JOIN CUSTOMER AS c 
    ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID";



回答3:


I puts SQL string into resource files, it allows easy edit multiline queries, and provides strongly typed named access to that queries even with IntelliSence tooltips.




回答4:


Why not option 3:

"Select bla bla bla"
"bla bla bla"
"...."

one long literal, split to many lines.




回答5:


I always use the second method as it is much faster. You use up too many lines of code with the first method, leading to a larger overhead.




回答6:


Have you thought about using LINQ?
http://blogs.msdn.com/b/matt/archive/2008/09/09/sql-ce-3-5-with-linq-to-sql.aspx. http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.coalesce.aspx




回答7:


I'm used to this approach:

  1. use +, this operator will add string at compile, not run time (https://docs.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings)
  2. this syntax shouldn't change at run time, use const.
  3. use indent to format script well.

The goal purpose is keeping readability and reduces data transfer, and there's no run-time performance cost :) , perfect.

    string const query = 
    "SELECT c.CUSTOMER_ID, " +
        "COALESCE (c.FIRST_NAME, ''_ + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME, " +
        "ct.NAME as CUSTOMER_TYPE " +
    "FROM CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c " +
        "ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID;";


来源:https://stackoverflow.com/questions/3176229/what-is-the-best-practice-for-writing-sql-queries-inside-c-sharp-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!