2017-07-20 3 views
0

この長いスクリプトのフォーマットソリューションを尋ねることはできますか?私はここでASPの新機能は、PHPでは長いスクリプトを宣言するときにPHPで異なるため、コピー&ペーストするだけで大​​丈夫です。しかし、ASPで。スクリプトは長い行にする必要があります。 サンプルスクリプトを問題asp.net要件を解決する変数(ASP.NET MVC RAZOR)に長いクエリを貼り付けたときにエラーが発生します

サンプルスクリプト

String sql = "SELECT 
    timesheet.date, 

    MAX(CASE 
    WHEN timein1 = 8 and note ='HOLIDAY' and counter = 1 
    then (SELECT description FROM holidays WHERE timesheet.date = holidays.date) 
    WHEN timein1 = 8 and note ='SICK LEAVE' and counter =1 then 'SICK LEAVE' 
    WHEN timein1 = 8 and note ='VACATION LEAVE' and counter =1 then 'VACATION LEAVE' 
    WHEN timein1 = 8 and note ='REGULAR LOGGED' and counter =1 then log 
    END) as note1, 


    MAX(case 
    WHEN timeout1 = 12 and note ='HOLIDAY' and counter =2 
    then (SELECT description FROM holidays WHERE timesheet.date = holidays.date) 
    WHEN timeout1 = 12 and note ='SICK LEAVE' and counter =2 then 'SICK LEAVE' 
    WHEN timeout1 = 12 and note ='VACATION LEAVE' and counter =2 then 'VACATION LEAVE' 
    WHEN timeout1 = 12 and note ='REGULAR LOGGED' and counter =2 then log 
    else '' 
    end) as note2, 

    MAX(case 
    WHEN timein2 = 13 and note ='HOLIDAY' and counter =3 
    then (SELECT description FROM holidays WHERE timesheet.date = holidays.date) 
    WHEN timein2 = 13 and note ='SICK LEAVE' and counter =3 then 'SICK LEAVE' 
    WHEN timein2 = 13 and note ='VACATION LEAVE' and counter =3 then 'VACATION LEAVE' 
    WHEN timein2 = 13 and note ='REGULAR LOGGED' and counter =3 then log 
    else '' 
    end) as note3, 

    MAX(case 
    WHEN timeout2 = 17 and note ='HOLIDAY' and counter =4 
    then (SELECT description FROM holidays WHERE timesheet.date = holidays.date) 
    WHEN timeout2 = 17 and note ='SICK LEAVE' and counter =4 then 'SICK LEAVE' 
    WHEN timeout2 = 17 and note ='VACATION LEAVE' and counter =4 then 'VACATION LEAVE' 
    WHEN timeout2 = 17 and note ='REGULAR LOGGED' and counter =4 then log 
    else '' 
    end) as note4 


FROM timesheet 

LEFT JOIN schedules ON timesheet.empid = schedules.empid 

WHERE timesheet.empid='40' and YEAR(timesheet.date) = 2017 

AND MONTH(timesheet.date) = 5 

AND timesheet.date <= CURDATE() 

AND timeStatus='OK' 

GROUP BY timesheet.date,timesheet.empid " 

この

SELECT timeshet.date、MAX(CASE timein1 = 8であり、= 'HOLIDAY' およびカウンタを注意します= 1などなどなど............長い文字列を連結するには3一般的な方法があります

+0

以下に述べるように、@を使用することができます。 –

+0

ストアプロシージャを作成すると、そのストアプロシージャを実行するだけで済みます。とても清潔です。 –

+0

リテラル文字列 'String sql = @" SELECT ... "を使用して、クエリを複数の行にまとめます。しかし、私はORM/'SqlConnection'からストアドプロシージャと呼び出しを作成する方が好きです。 –

答えて

1

おかげでみんな..:

  1. verbatim string literal(二重引用符の前に@という記号が付いています)を使用してください。 例:すべての行のためのconcatenation operator (+)を使用して

    String sql = @"SELECT timesheet.date, MAX(CASE WHEN timein1 = 8 
           and note ='HOLIDAY' and counter = 1 ..."; 
    
  2. 。 例:複数のコマンドのためのStringBuilderを使用して

    String sql = "SELECT timesheet.date, MAX(CASE WHEN timein1 = 8 and note ='HOLIDAY' and counter = 1 ..." + 
          ... + 
          "GROUP BY timesheet.date,timesheet.empid"; 
    
  3. 。 例:

    var sb = new StringBuilder(); 
    sb.Append("SELECT timesheet.date,"); 
    sb.Append("MAX(CASE WHEN timein1 = 8 and note ='HOLIDAY' and counter = 1 ..."); 
    // other append methods 
    
    String sql = sb.ToString(); 
    

それが唯一の("のために例えば""、エスケープする必要がある唯一の二重引用符記号)、別の二重引用符で区切られたので、逐語的文字列メソッドは、すべての最も効率的です。

あなたが他の変数の値を文字列連結を実行したい場合、あなたはこのように書式をそのまま文字列&複合と組み合わせるString.Format使用する必要があります。

String sql = String.Format(@"SELECT ... 
          FROM timesheet LEFT JOIN schedules ON timesheet.empid = schedules.empid 
          WHERE timesheet.empid='40' AND YEAR(timesheet.date) = {0} 
          AND MONTH(timesheet.date) = {1} 
          AND timesheet.date <= CURDATE() 
          AND timeStatus='OK' 
          GROUP BY timesheet.date,timesheet.empid", List, List_month); 

NBを:あなたがそのような長いクエリ文字列を持っている場合は、私が

CREATE DEFINER = [definer_name] PROCEDURE [procedure_name] 
-- [parameters if any] 
BEGIN 
    SELECT timesheet.date, MAX(CASE WHEN timein1 = 8 and note ='HOLIDAY' and counter = 1 ... 
END; 

そしてMySQL database connectionでプロシージャを呼び出す:このようなstored procedureを作成することを好む

using (var connection = new MySqlConnection("[MySQL_connection_string]")) 
{ 
    try 
    { 
     connection.Open(); 
     using (var command = new MySqlCommand("[procedure_name]", connection)) 
     { 
      command.CommandType = CommandType.StoredProcedure; 
      // add parameters here 

      var reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       // reading data from database 
      } 

      // other stuff 
     } 
     connection.Close(); 
    } 
    catch (Exception e) 
    { 
     // handle exceptions here 
    } 
} 
+0

助けてくれてありがとう.. –

+0

AND MONTH(タイムシート。+ time_reset = 'OK'これは改行定数を使用する必要があることを示します。 + List_month + –

+0

'String.Format(@" SELECT ... AND MONTH(timesheet.date)= '{0}' AND ... "、List_month)'を使ってください。他の変数からの文字列連結を使用している場合は、文字列リテラル&[複合フォーマット]と一緒に 'String.Format'を使用してください(https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-書式設定)を使用して1つの文字列に結合します。 –

0

あなたは単にあなたが連結演算子** + **を使用することができます

String sql = @"SELECT 
    timesheet.date, 

    MAX(CASE 
    WHEN timein1 = 8 and note ='HOLIDAY' and counter = 1 
    then (SELECT description FROM holidays WHERE timesheet.date = holidays.date) 
    WHEN timein1 = 8 and note ='SICK LEAVE' and counter =1 then 'SICK LEAVE' 
    WHEN timein1 = 8 and note ='VACATION LEAVE' and counter =1 then 'VACATION LEAVE' 
    WHEN timein1 = 8 and note ='REGULAR LOGGED' and counter =1 then log 
    END) as note1, 


    MAX(case 
    WHEN timeout1 = 12 and note ='HOLIDAY' and counter =2 
    then (SELECT description FROM holidays WHERE timesheet.date = holidays.date) 
    WHEN timeout1 = 12 and note ='SICK LEAVE' and counter =2 then 'SICK LEAVE' 
    WHEN timeout1 = 12 and note ='VACATION LEAVE' and counter =2 then 'VACATION LEAVE' 
    WHEN timeout1 = 12 and note ='REGULAR LOGGED' and counter =2 then log 
    else '' 
    end) as note2, 

    MAX(case 
    WHEN timein2 = 13 and note ='HOLIDAY' and counter =3 
    then (SELECT description FROM holidays WHERE timesheet.date = holidays.date) 
    WHEN timein2 = 13 and note ='SICK LEAVE' and counter =3 then 'SICK LEAVE' 
    WHEN timein2 = 13 and note ='VACATION LEAVE' and counter =3 then 'VACATION LEAVE' 
    WHEN timein2 = 13 and note ='REGULAR LOGGED' and counter =3 then log 
    else '' 
    end) as note3, 

    MAX(case 
    WHEN timeout2 = 17 and note ='HOLIDAY' and counter =4 
    then (SELECT description FROM holidays WHERE timesheet.date = holidays.date) 
    WHEN timeout2 = 17 and note ='SICK LEAVE' and counter =4 then 'SICK LEAVE' 
    WHEN timeout2 = 17 and note ='VACATION LEAVE' and counter =4 then 'VACATION LEAVE' 
    WHEN timeout2 = 17 and note ='REGULAR LOGGED' and counter =4 then log 
    else '' 
    end) as note4 


FROM timesheet 

LEFT JOIN schedules ON timesheet.empid = schedules.empid 

WHERE timesheet.empid='40' and YEAR(timesheet.date) = 2017 

AND MONTH(timesheet.date) = 5 

AND timesheet.date <= CURDATE() 

AND timeStatus='OK' 

GROUP BY timesheet.date,timesheet.empid " 
関連する問題