2017-08-29 15 views
1

何が起こっているのか理解できますか?私はもちろんの ステートメントを使用する際に変数とSQL Serverを使用する

  • NQFレベル
  • クレジット
    1. SAQAのIDを取得しようとしているが、私は理解していないものがあります。

      私は私のクエリで変数を作成した場合など

      public Int32 T_Course_Id = 0, T_Company_Id = 0, T_Nqf = 0, T_Credit = 0; 
      
      string queryTaskId = "SELECT [course_saqa_id] FROM"+ 
              "[sta].[dbo].[Courses]"+ 
              "WHERE course_name = '" + _Coursename + "'"; 
      
      string queryNqf = "SELECT [course_nqf]"+ 
              "FROM [sta].[dbo].[Courses]"+ 
              "WHERE course_saqa_id = '" + T_Course_Id + "'"; 
      
      using (SqlConnection Conn = new SqlConnection(ConnString)) 
      { 
          Conn.Open(); 
      
          using (SqlCommand command = new SqlCommand(queryTaskId, Conn)) 
          { 
           using (SqlDataReader reader = command.ExecuteReader()) 
           { 
            if (reader.HasRows) 
            { 
             reader.Read(); 
             // Call Read before accessing data. 
             T_Course_Id = reader.GetInt32(0); 
            } 
      
            // Call Close when done reading. 
            reader.Close(); 
           } 
          } 
      
          using (SqlCommand command = new SqlCommand(queryCredit, Conn)) 
          { 
           using (SqlDataReader reader = command.ExecuteReader()) 
           { 
            if (reader.HasRows) 
            { 
             reader.Read(); 
      
             // Call Read before accessing data. 
             T_Credit = reader.GetInt32(0); 
            } 
      
            // Call Close when done reading. 
            reader.Close(); 
           } 
          }      
      
          Conn.Close(); 
      } 
      

      私はT_Credit変数の0値を得るが、私は(このようにそれを行う場合にのみ、最後で、このようにそれを行うと一部)あなたは、私が代わりに変数

      using (SqlCommand command = new SqlCommand("SELECT [course_nqf] FROM [sta].[dbo].[Courses] WHERE course_saqa_id = '" + T_Course_Id + "'", Conn)) 
      
      の直接SQLコマンドを渡している見ることができるように

      using (SqlCommand command = new SqlCommand("SELECT [course_nqf] FROM [sta].[dbo].[Courses] WHERE course_saqa_id = '" + T_Course_Id + "'", Conn)) 
      { 
          using (SqlDataReader reader = command.ExecuteReader()) 
          { 
           if (reader.HasRows) 
           { 
            reader.Read(); 
            // Call Read before accessing data. 
            T_Credit = reader.GetInt32(0); 
           } 
      
           // Call Close when done reading. 
           reader.Close(); 
          } 
      }      
      

      、私は、正しい値を取得します

      ここで変数が動作しないのはなぜですか?

    +0

    は、テンプレート文字列を使用してみましたか? C#6 また、 'using(){...}'スタイルを使用しているときにcloseを呼び出すのはなぜですか? –

    +1

    これはちょうどタイプミスで、最初のケースでは間違った変数を使用しています。 'queryCredit'の代わりに' queryNqf'を使うべきです。 – Alisson

    +0

    [SQLインジェクション警告](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29。aspx) - あなたはSQLステートメントを連結しない** ** SQLインジェクションを避けるため**代わりに**パラメータ化されたクエリを使用する - [Little Bobby Tables](https://xkcd.com/327/)をチェックしてください –

    答えて

    4
    を使用して

    オペレーションの重要事項。あなたの最初の例で

    その後、直後のクエリ文字列を作成し、あなたのT_Course_ID = 0の値を設定しています。その時点で、T_Course_IDの値は0と評価され、忘れられています。文字列連結は使用されるたびに評価されません。譲渡時にのみ。

    T_Course_IDには、最初の検索で新しい値が割り当てられます。 SQLCommandは、変数の再割り当て後に実行されるため、毎回新しい値を使用して評価されます。ここで

    ではJavaScriptを使用しての例で、同じことがよくありました適用されます。

    var T_Course_Id = 0; 
     
    
     
    var queryNqf = 
     
        "SELECT [course_nqf]" + 
     
        "FROM [sta].[dbo].[Courses]" + 
     
        "WHERE course_saqa_id = '" + T_Course_Id + "'"; 
     
    
     
    // Query displays initialized value 
     
    console.log(queryNqf); 
     
    
     
    // Variable is changed 
     
    T_Course_Id = 'I Will NOT CHANGE'; 
     
    
     
    // Try to log the updated value but it doesn't work because queryNqf was assigned too early 
     
    console.log(queryNqf); 
     
    
     
    // Reassign with the new value 
     
    queryNqf = 
     
        "SELECT [course_nqf]" + 
     
        "FROM [sta].[dbo].[Courses]" + 
     
        "WHERE course_saqa_id = '" + T_Course_Id + "'"; 
     
    
     
    // Retrieve the expect result. 
     
    console.log(queryNqf);

    0

    あなたはusingステートメントで正しいコマンド文字列queryNqfを使用していないようです。

    ユアーズ:(SqlCommandオブジェクトのコマンド=新しいSqlCommandオブジェクト(queryCredit、コネティカット))

    を使用してことができます:(SqlCommandオブジェクトのコマンド=新しいSqlCommandオブジェクト(queryNqf、コネティカット))

    関連する問題