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. C#
  4. passing multiple parameters to the query in C#

passing multiple parameters to the query in C#

Scheduled Pinned Locked Moved C#
csharpdatabasetutorialquestionannouncement
5 Posts 5 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.
  • D Offline
    D Offline
    Deepali Khalkar
    wrote on last edited by
    #1

    I have tried with the following parameterised query for select statement and is working fine. SqlCeConnection conn; SqlCeCommand comm; comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "select * from signuptbl where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = textBox1 .Text ; comm.Parameters.Add(para); Now i want to write an update query which takes multiple parameters. I have thought of the following code. SqlCeConnection conn; SqlCeCommand comm; comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "update signuptbl set firstname=@fname,lastname=@lname,,psw=@psw1 where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = textBox1 .Text ; comm.Parameters.Add(para); para.ParameterName = "@psw"; para.Value = textBox2 .Text ; comm.Parameters.Add(para); para.ParameterName = "@fname"; para.Value = textBox3 .Text ; comm.Parameters.Add(para); para.ParameterName = "@lname"; para.Value = textBox3 .Text ; comm.Parameters.Add(para); I have tried this code but the row was not updated. Is this a proper way? How to pass multiple parameters in a query? Can i get a sample code for the same. Thanks in advance.. :)

    N M F 3 Replies Last reply
    0
    • D Deepali Khalkar

      I have tried with the following parameterised query for select statement and is working fine. SqlCeConnection conn; SqlCeCommand comm; comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "select * from signuptbl where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = textBox1 .Text ; comm.Parameters.Add(para); Now i want to write an update query which takes multiple parameters. I have thought of the following code. SqlCeConnection conn; SqlCeCommand comm; comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "update signuptbl set firstname=@fname,lastname=@lname,,psw=@psw1 where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = textBox1 .Text ; comm.Parameters.Add(para); para.ParameterName = "@psw"; para.Value = textBox2 .Text ; comm.Parameters.Add(para); para.ParameterName = "@fname"; para.Value = textBox3 .Text ; comm.Parameters.Add(para); para.ParameterName = "@lname"; para.Value = textBox3 .Text ; comm.Parameters.Add(para); I have tried this code but the row was not updated. Is this a proper way? How to pass multiple parameters in a query? Can i get a sample code for the same. Thanks in advance.. :)

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      You have created only one parameter and overwriting it's properties each time. You need separate SqlCeParameter instances for each parameter.

      comm.Parameters.AddWithValue("@uname",textBox1 .Text);
      comm.Parameters.AddWithValue("@psw",textBox2 .Text);
      ...

      AddWithValue[^] will create SqlCeParameter internally and add it to the Parameters collection. :)

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • D Deepali Khalkar

        I have tried with the following parameterised query for select statement and is working fine. SqlCeConnection conn; SqlCeCommand comm; comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "select * from signuptbl where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = textBox1 .Text ; comm.Parameters.Add(para); Now i want to write an update query which takes multiple parameters. I have thought of the following code. SqlCeConnection conn; SqlCeCommand comm; comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "update signuptbl set firstname=@fname,lastname=@lname,,psw=@psw1 where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = textBox1 .Text ; comm.Parameters.Add(para); para.ParameterName = "@psw"; para.Value = textBox2 .Text ; comm.Parameters.Add(para); para.ParameterName = "@fname"; para.Value = textBox3 .Text ; comm.Parameters.Add(para); para.ParameterName = "@lname"; para.Value = textBox3 .Text ; comm.Parameters.Add(para); I have tried this code but the row was not updated. Is this a proper way? How to pass multiple parameters in a query? Can i get a sample code for the same. Thanks in advance.. :)

        M Offline
        M Offline
        Megidolaon
        wrote on last edited by
        #3

        An easy way is to only pass one string that is the full query. You simply construct it by using variables in C# to read the input and then adding it to the query string. You might have to add brackets and commas to the query string after the variables have been added.

        J 1 Reply Last reply
        0
        • M Megidolaon

          An easy way is to only pass one string that is the full query. You simply construct it by using variables in C# to read the input and then adding it to the query string. You might have to add brackets and commas to the query string after the variables have been added.

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Worst answer you could have given, you've just told the OP to take aa step backwards to dynamically-built sql queries rather than using parameters. You should read up on SQL Injection attacks.

          1 Reply Last reply
          0
          • D Deepali Khalkar

            I have tried with the following parameterised query for select statement and is working fine. SqlCeConnection conn; SqlCeCommand comm; comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "select * from signuptbl where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = textBox1 .Text ; comm.Parameters.Add(para); Now i want to write an update query which takes multiple parameters. I have thought of the following code. SqlCeConnection conn; SqlCeCommand comm; comm = new SqlCeCommand(); comm.Connection = conn; comm.CommandText = "update signuptbl set firstname=@fname,lastname=@lname,,psw=@psw1 where username =@uname"; SqlCeParameter para = new SqlCeParameter(); para.ParameterName = "@uname"; para.Value = textBox1 .Text ; comm.Parameters.Add(para); para.ParameterName = "@psw"; para.Value = textBox2 .Text ; comm.Parameters.Add(para); para.ParameterName = "@fname"; para.Value = textBox3 .Text ; comm.Parameters.Add(para); para.ParameterName = "@lname"; para.Value = textBox3 .Text ; comm.Parameters.Add(para); I have tried this code but the row was not updated. Is this a proper way? How to pass multiple parameters in a query? Can i get a sample code for the same. Thanks in advance.. :)

            F Offline
            F Offline
            fly904
            wrote on last edited by
            #5

            In most of my code I pass the parameters as a variable to another function which would make the code look like this:

            SqlCeParameter[] sqlParams = new SqlCeParameter[4];

            sqlParams[0] = new SqlCeParameter("@uname", textbox1.Text);
            ...

            comm.Parameters.AddRange(sqlParams);

            hmmm pie

            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