Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Removing the concatenated code variable for a SQL select statement .

Removing the concatenated code variable for a SQL select statement .

Scheduled Pinned Locked Moved Database
databasequestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stephen Holdorf
    wrote on last edited by
    #1

    I know I should use a stored procedure to do this but ths is not possible. In the code below return values from the "select" statements are being assigned to code variable values being concatenated with the actual Select statement. I am not talking about the code in the Where clause but the code in the "Select" portion. See the string as below:

    query.Append(" SELECT e581_key, 'e581', 'rnlt', rnlt, '" + _rnlt + "' , 'SaveEditRNLT', '" + _logUserName + "', getdate() ");
    query.Append(" FROM e581 ");

    The select statement is then assigned to the DbCommand.CommandText of the command.ExecuteNonQuery.

    How do I move the actual code variables being concatenated to some type of command parameter?

    S Richard DeemingR 2 Replies Last reply
    0
    • S Stephen Holdorf

      I know I should use a stored procedure to do this but ths is not possible. In the code below return values from the "select" statements are being assigned to code variable values being concatenated with the actual Select statement. I am not talking about the code in the Where clause but the code in the "Select" portion. See the string as below:

      query.Append(" SELECT e581_key, 'e581', 'rnlt', rnlt, '" + _rnlt + "' , 'SaveEditRNLT', '" + _logUserName + "', getdate() ");
      query.Append(" FROM e581 ");

      The select statement is then assigned to the DbCommand.CommandText of the command.ExecuteNonQuery.

      How do I move the actual code variables being concatenated to some type of command parameter?

      S Offline
      S Offline
      Sascha Lefevre
      wrote on last edited by
      #2

      You can't use SQL-parameters for column names. But if the value for these variables can not come from user input, then there's no risk of SQL-injection (I assume that's why you're asking). If you can guarantee this, then you'll be fine here. But there are some other strange things: - You're selecting the column "rnlt" at least twice. - You're trying to select a column that has the name of a (the current?) date? - "e581" seems to be a column name and a table name?

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      1 Reply Last reply
      0
      • S Stephen Holdorf

        I know I should use a stored procedure to do this but ths is not possible. In the code below return values from the "select" statements are being assigned to code variable values being concatenated with the actual Select statement. I am not talking about the code in the Where clause but the code in the "Select" portion. See the string as below:

        query.Append(" SELECT e581_key, 'e581', 'rnlt', rnlt, '" + _rnlt + "' , 'SaveEditRNLT', '" + _logUserName + "', getdate() ");
        query.Append(" FROM e581 ");

        The select statement is then assigned to the DbCommand.CommandText of the command.ExecuteNonQuery.

        How do I move the actual code variables being concatenated to some type of command parameter?

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        You need to pass the variables as parameters, in exactly the same way as you would for the WHERE clause:

        using (var command = new SqlCommand("", connection))
        {
        var query = new StringBuilder();
        ...
        query.Append(" SELECT e581_key, 'e581', 'rnlt', rnlt, @rnlt, 'SaveEditRNLT', @logUserName, getdate() ");
        query.Append(" FROM e581 ");
        command.Parameters.AddWithValue("@rnlt", _rnlt);
        command.Parameters.AddWithValue("@logUserName", _logUserName);
        ...
        command.CommandText = query.ToString();
        ...
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        S 2 Replies Last reply
        0
        • Richard DeemingR Richard Deeming

          You need to pass the variables as parameters, in exactly the same way as you would for the WHERE clause:

          using (var command = new SqlCommand("", connection))
          {
          var query = new StringBuilder();
          ...
          query.Append(" SELECT e581_key, 'e581', 'rnlt', rnlt, @rnlt, 'SaveEditRNLT', @logUserName, getdate() ");
          query.Append(" FROM e581 ");
          command.Parameters.AddWithValue("@rnlt", _rnlt);
          command.Parameters.AddWithValue("@logUserName", _logUserName);
          ...
          command.CommandText = query.ToString();
          ...
          }


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          S Offline
          S Offline
          Stephen Holdorf
          wrote on last edited by
          #4

          I don't understand what you mean. for instance take the code below:

          query.Append("SELECT e581_key, 'e581', 'pickup_date', pickup_date , '" + _dateMaterialRequired + "' , 'SaveEditPickup', '" + _logUserName + "', getdate() ");

          Now _dateMaterialRequired and _timeMaterialRequired are variables, properties, that belong to the to the class that called the hard coded SQL statement. When the SQL is called how are the select statement return values assigned to the variables, properties, that are in the calling code?

          Richard DeemingR 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            You need to pass the variables as parameters, in exactly the same way as you would for the WHERE clause:

            using (var command = new SqlCommand("", connection))
            {
            var query = new StringBuilder();
            ...
            query.Append(" SELECT e581_key, 'e581', 'rnlt', rnlt, @rnlt, 'SaveEditRNLT', @logUserName, getdate() ");
            query.Append(" FROM e581 ");
            command.Parameters.AddWithValue("@rnlt", _rnlt);
            command.Parameters.AddWithValue("@logUserName", _logUserName);
            ...
            command.CommandText = query.ToString();
            ...
            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            S Offline
            S Offline
            Stephen Holdorf
            wrote on last edited by
            #5

            I don't understand what you mean. for instance take the code below:

            query.Append(" SELECT e581_key, 'e581', 'alloc_period', alloc_period , '" + _allocationPeriod.ToString() + "' , 'SaveEditCombatLoad', '" + _logUserName + "' , getdate() ");
            query.Append(" FROM e581 ");

            Now _allocationPeriod.ToString() and SaveEditCombatLoad are variables, properties, that belong to the to the class that called the hard coded SQL statement. When you say columns returns by the SQL no columns exist It's just values in the class. Am I missing something??

            1 Reply Last reply
            0
            • S Stephen Holdorf

              I don't understand what you mean. for instance take the code below:

              query.Append("SELECT e581_key, 'e581', 'pickup_date', pickup_date , '" + _dateMaterialRequired + "' , 'SaveEditPickup', '" + _logUserName + "', getdate() ");

              Now _dateMaterialRequired and _timeMaterialRequired are variables, properties, that belong to the to the class that called the hard coded SQL statement. When the SQL is called how are the select statement return values assigned to the variables, properties, that are in the calling code?

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              What? :confused: The code you've shown passes the value of the variables from C# to SQL - using string concatenation, so it's vulnerable to SQL Injection. Those values are returned as part of the result-set of the query. Nothing in the code you've shown will update the value of the C# variables.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              S 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                What? :confused: The code you've shown passes the value of the variables from C# to SQL - using string concatenation, so it's vulnerable to SQL Injection. Those values are returned as part of the result-set of the query. Nothing in the code you've shown will update the value of the C# variables.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                S Offline
                S Offline
                Stephen Holdorf
                wrote on last edited by
                #7

                In the select statement I did not include the "Where" clause by mistake. But these values are being filled in by the Where clause. Sorry.

                Richard DeemingR 1 Reply Last reply
                0
                • S Stephen Holdorf

                  In the select statement I did not include the "Where" clause by mistake. But these values are being filled in by the Where clause. Sorry.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  No, that still doesn't make any sense.

                  holdorf wrote:

                  how are the select statement return values assigned to the variables, properties, that are in the calling code?

                  holdorf wrote:

                  But these values are being filled in by the Where clause.

                  Executing a SQL query will never automatically update the value of a C# variable.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  S 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    No, that still doesn't make any sense.

                    holdorf wrote:

                    how are the select statement return values assigned to the variables, properties, that are in the calling code?

                    holdorf wrote:

                    But these values are being filled in by the Where clause.

                    Executing a SQL query will never automatically update the value of a C# variable.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    S Offline
                    S Offline
                    Stephen Holdorf
                    wrote on last edited by
                    #9

                    I guess I am not making myself clear. This is working SQL code that update the C# variables from the query after it is called. What my job is to remove all of the SQL inject errors that exist from this working code.

                    Richard DeemingR 1 Reply Last reply
                    0
                    • S Stephen Holdorf

                      I guess I am not making myself clear. This is working SQL code that update the C# variables from the query after it is called. What my job is to remove all of the SQL inject errors that exist from this working code.

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      No, what you have posted is working-but-vulnerable code which executes a query that includes the values of some C# variables in the results. At no point does the code you've posted update the value of any C# variables. Either you've forgotten to post that part of the code, or the code isn't doing what you think it's doing.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups