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. how to insert variable values in SQL server using INSERT INTO statement?

how to insert variable values in SQL server using INSERT INTO statement?

Scheduled Pinned Locked Moved Database
c++databasequestionsql-serversysadmin
9 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.
  • M Offline
    M Offline
    MahaKh
    wrote on last edited by
    #1

    i want to insert values (into database table) which are calculated from an expression and is stored in a variable, in INSERT INTO statement, how can i do this? i m doing this in VC++6 MFC using ODBC constant values goes perfectally in database<

    R P 2 Replies Last reply
    0
    • M MahaKh

      i want to insert values (into database table) which are calculated from an expression and is stored in a variable, in INSERT INTO statement, how can i do this? i m doing this in VC++6 MFC using ODBC constant values goes perfectally in database<

      R Offline
      R Offline
      RaviRanjanKr
      wrote on last edited by
      #2

      What you have tried about to insert value you have no mention whatever, You can easily insert value into your database table by using

      Insert into tablename values(your value depend upon table column data type)

      1 Reply Last reply
      0
      • M MahaKh

        i want to insert values (into database table) which are calculated from an expression and is stored in a variable, in INSERT INTO statement, how can i do this? i m doing this in VC++6 MFC using ODBC constant values goes perfectally in database<

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Use parameters.

        M 1 Reply Last reply
        0
        • P PIEBALDconsult

          Use parameters.

          M Offline
          M Offline
          MahaKh
          wrote on last edited by
          #4

          for example here is some part of my code: string str="Birthday"; i have to insert this str variable using: INTERT into mytable VALUES(str) ; how can i insert this??? n i dont know how to use parameters...plz tell me some very easy way to do this...:(

          P R M 3 Replies Last reply
          0
          • M MahaKh

            for example here is some part of my code: string str="Birthday"; i have to insert this str variable using: INTERT into mytable VALUES(str) ; how can i insert this??? n i dont know how to use parameters...plz tell me some very easy way to do this...:(

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            It partly depends on what language you host your SQL in, also on which database system you use. INSERT INTO table (field0, field1 ...) VALUES (@Param0, @Param1 ... ) Then create parameters. Then, as you loop through the data to insert, set the parameters and execute the statement.

            1 Reply Last reply
            0
            • M MahaKh

              for example here is some part of my code: string str="Birthday"; i have to insert this str variable using: INTERT into mytable VALUES(str) ; how can i insert this??? n i dont know how to use parameters...plz tell me some very easy way to do this...:(

              R Offline
              R Offline
              RaviRanjanKr
              wrote on last edited by
              #6

              I let you from the beginning step by step to insert data into table Create a connection by using SqlConnection class as

              SqlConnection con = new SqlConnection("connectionString") // Connectionstring is responsible to store info to connect database.

              then, you need to create Command object by SqlCommand Class as

              string str = INTERT into mytable VALUES('"+str+"');
              SqlCommand cmd = new SqlCommand(str,con); // str is responsible to hold you insert command and con is object of connection

              then you need to execute query by using ExecuteNonQuery Method as

              cmd.ExecuteNonQuery();

              This is easiest way to insert data into table. Hope it will work for you.. :)

              M 1 Reply Last reply
              0
              • M MahaKh

                for example here is some part of my code: string str="Birthday"; i have to insert this str variable using: INTERT into mytable VALUES(str) ; how can i insert this??? n i dont know how to use parameters...plz tell me some very easy way to do this...:(

                M Offline
                M Offline
                MDNadeemAkhter
                wrote on last edited by
                #7

                if you don;t want to use parameter then simple make Connction and Command and Execute your command it will save your data.

                P 1 Reply Last reply
                0
                • R RaviRanjanKr

                  I let you from the beginning step by step to insert data into table Create a connection by using SqlConnection class as

                  SqlConnection con = new SqlConnection("connectionString") // Connectionstring is responsible to store info to connect database.

                  then, you need to create Command object by SqlCommand Class as

                  string str = INTERT into mytable VALUES('"+str+"');
                  SqlCommand cmd = new SqlCommand(str,con); // str is responsible to hold you insert command and con is object of connection

                  then you need to execute query by using ExecuteNonQuery Method as

                  cmd.ExecuteNonQuery();

                  This is easiest way to insert data into table. Hope it will work for you.. :)

                  M Offline
                  M Offline
                  MahaKh
                  wrote on last edited by
                  #8

                  thanks a lot RaviRanjankr! for ur concern...but i have tried this is a simple one...actually + sign is not recognized in the query, it gives syntex error...but n e ways thanks a lot my problem has been solved now,thanks

                  1 Reply Last reply
                  0
                  • M MDNadeemAkhter

                    if you don;t want to use parameter then simple make Connction and Command and Execute your command it will save your data.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    Please don't give people bad advice like this. You've basically just advised the poster that they should leave themselves wide open to a SQL Injection attack, and that's not cool.

                    I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Onyx

                    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