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. General Programming
  3. Visual Basic
  4. What does the @ do in the following statement?

What does the @ do in the following statement?

Scheduled Pinned Locked Moved Visual Basic
databasequestionannouncement
4 Posts 4 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.
  • K Offline
    K Offline
    kenn_rosie
    wrote on last edited by
    #1

    What does the @ do in the following statement? Is this used to pick the particular DB field associated with the row in the update statement? Dim strSQL As String = _ "UPDATE [Products] SET [ProductName] = @ProdName, " & _ "[UnitPrice] = @UnitPrice, [ProductDescription] = @ProdDesc, [LastUpdated] = @LastUpdated " & _ "WHERE [ProductID] = @ProductID"

    C G 2 Replies Last reply
    0
    • K kenn_rosie

      What does the @ do in the following statement? Is this used to pick the particular DB field associated with the row in the update statement? Dim strSQL As String = _ "UPDATE [Products] SET [ProductName] = @ProdName, " & _ "[UnitPrice] = @UnitPrice, [ProductDescription] = @ProdDesc, [LastUpdated] = @LastUpdated " & _ "WHERE [ProductID] = @ProductID"

      C Offline
      C Offline
      Craster
      wrote on last edited by
      #2

      That's SQL stored procedure syntax. The items with '@' in front refer to parameters being supplied to a stored procedure. The only reason you should ever see this in VB code is if the code is attempting to create the stored procedure on the SQL Server - I can't see that this would ever work if you attempt to run it as an update statement directly from a VB app. It's possible that someone's copied and pasted the code from a stored procedure directly into VB as an attempt to do an update, but I don't believe it can possibly work.

      C 1 Reply Last reply
      0
      • K kenn_rosie

        What does the @ do in the following statement? Is this used to pick the particular DB field associated with the row in the update statement? Dim strSQL As String = _ "UPDATE [Products] SET [ProductName] = @ProdName, " & _ "[UnitPrice] = @UnitPrice, [ProductDescription] = @ProdDesc, [LastUpdated] = @LastUpdated " & _ "WHERE [ProductID] = @ProductID"

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        No, they are parameter names. When excuting the query, you also have to add parameter objects to the command object for each parameter in the query. Example: command.Parameters.Add("@ProductID", SqlDbType.Int).Value = 42; --- b { font-weight: normal; }

        1 Reply Last reply
        0
        • C Craster

          That's SQL stored procedure syntax. The items with '@' in front refer to parameters being supplied to a stored procedure. The only reason you should ever see this in VB code is if the code is attempting to create the stored procedure on the SQL Server - I can't see that this would ever work if you attempt to run it as an update statement directly from a VB app. It's possible that someone's copied and pasted the code from a stored procedure directly into VB as an attempt to do an update, but I don't believe it can possibly work.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Craster wrote:

          The items with '@' in front refer to parameters being supplied to a stored procedure.

          Among other things. See also Guffa's reply to the Original Poster.

          Craster wrote:

          The only reason you should ever see this in VB code is if the code is attempting to create the stored procedure on the SQL Server - I can't see that this would ever work if you attempt to run it as an update statement directly from a VB app.

          This is incorrect.

          Craster wrote:

          It's possible that someone's copied and pasted the code from a stored procedure directly into VB as an attempt to do an update, but I don't believe it can possibly work.

          Yes, it can work. You pass parameters for any SQL Statement through the SqlCommand object. For example. The SQL:

          SELECT * FROM MyTable WHERE name=@name

          Can be accessed in VB as:

          Dim cmd As SqlCommand
          cmd = New SqlCommand()
          cmd.CommandText="SELECT * FROM MyTable WHERE name=@name"
          cmd.Parameters.Add("@name", "John Smith")
          cmd.Connection = MyConnection
          Dim reader As SqlDataReader
          reader = cmd.ExecuteReader()

          ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

          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