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. SQL statement

SQL statement

Scheduled Pinned Locked Moved Database
databasejavaarchitecturehelplearning
10 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.
  • T Offline
    T Offline
    Tichaona J
    wrote on last edited by
    #1

    Hi I am using a SQL statement to access data from a database, if I hardcode a specific name in the field I get, the name being searched for is returned and displayed. Of-course this isn't a convient way of doing things so I introduced a text box to take in the data, store it in a variable then, this is were my problem lies, I try and used my variable in the SQL statement but it does not work. My code is as below: static private string GetConnectionString() { return "Driver={Microsoft Access Driver (*.mdb)};" + "Dbq=c:\\Users\\Tichaona\\Documents\\My Dropbox\\SET10101 Software Architecture\\coursework1\\patients.mdb;Uid=java;Pwd=password;"; } private void BtnSearch_Click(object sender, EventArgs e) { String nameSearch = TxtBxName.Text; <<

    T P 2 Replies Last reply
    0
    • T Tichaona J

      Hi I am using a SQL statement to access data from a database, if I hardcode a specific name in the field I get, the name being searched for is returned and displayed. Of-course this isn't a convient way of doing things so I introduced a text box to take in the data, store it in a variable then, this is were my problem lies, I try and used my variable in the SQL statement but it does not work. My code is as below: static private string GetConnectionString() { return "Driver={Microsoft Access Driver (*.mdb)};" + "Dbq=c:\\Users\\Tichaona\\Documents\\My Dropbox\\SET10101 Software Architecture\\coursework1\\patients.mdb;Uid=java;Pwd=password;"; } private void BtnSearch_Click(object sender, EventArgs e) { String nameSearch = TxtBxName.Text; <<

      T Offline
      T Offline
      thatraja
      wrote on last edited by
      #2

      Change the line

      string queryString = "SELECT Name FROM patients WHERE Name LIKE '%nameSearch%';";

      into

      string queryString = "SELECT Name FROM patients WHERE Name LIKE '%" + nameSearch + "%'";

      Use Code block instead of inline code for your code while posting here [Edit]As Piebal's suggestion, Use parameters with command object Click here[^][/Edit]

      thatraja |Chennai|India|


      Brainbench certifications
      Down-votes are like kid's kisses don't reject it :-)
      Do what you want quickly because the Doomsday on 2012 :-)

      modified on Thursday, November 25, 2010 9:50 AM

      T P R 3 Replies Last reply
      0
      • T thatraja

        Change the line

        string queryString = "SELECT Name FROM patients WHERE Name LIKE '%nameSearch%';";

        into

        string queryString = "SELECT Name FROM patients WHERE Name LIKE '%" + nameSearch + "%'";

        Use Code block instead of inline code for your code while posting here [Edit]As Piebal's suggestion, Use parameters with command object Click here[^][/Edit]

        thatraja |Chennai|India|


        Brainbench certifications
        Down-votes are like kid's kisses don't reject it :-)
        Do what you want quickly because the Doomsday on 2012 :-)

        modified on Thursday, November 25, 2010 9:50 AM

        T Offline
        T Offline
        Tichaona J
        wrote on last edited by
        #3

        Thanks it works great... :)

        P 1 Reply Last reply
        0
        • T Tichaona J

          Hi I am using a SQL statement to access data from a database, if I hardcode a specific name in the field I get, the name being searched for is returned and displayed. Of-course this isn't a convient way of doing things so I introduced a text box to take in the data, store it in a variable then, this is were my problem lies, I try and used my variable in the SQL statement but it does not work. My code is as below: static private string GetConnectionString() { return "Driver={Microsoft Access Driver (*.mdb)};" + "Dbq=c:\\Users\\Tichaona\\Documents\\My Dropbox\\SET10101 Software Architecture\\coursework1\\patients.mdb;Uid=java;Pwd=password;"; } private void BtnSearch_Click(object sender, EventArgs e) { String nameSearch = TxtBxName.Text; <<

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

          Use a parameter!

          1 Reply Last reply
          0
          • T thatraja

            Change the line

            string queryString = "SELECT Name FROM patients WHERE Name LIKE '%nameSearch%';";

            into

            string queryString = "SELECT Name FROM patients WHERE Name LIKE '%" + nameSearch + "%'";

            Use Code block instead of inline code for your code while posting here [Edit]As Piebal's suggestion, Use parameters with command object Click here[^][/Edit]

            thatraja |Chennai|India|


            Brainbench certifications
            Down-votes are like kid's kisses don't reject it :-)
            Do what you want quickly because the Doomsday on 2012 :-)

            modified on Thursday, November 25, 2010 9:50 AM

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

            Negative several million! Look up "SQL injection attack". :mad:

            T 1 Reply Last reply
            0
            • T Tichaona J

              Thanks it works great... :)

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

              Don't do it that way.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                Negative several million! Look up "SQL injection attack". :mad:

                T Offline
                T Offline
                thatraja
                wrote on last edited by
                #7

                PIEBALDconsult wrote:

                Negative several million! Look up "SQL injection attack".

                Yes dude it's true, But he is just expecting just correct the error. Also here before I received a rude comment from Enquirer for the same type of suggestion. Ok Here after I'll add the additional details along with answer. Thanks

                thatraja |Chennai|India|


                Brainbench certifications
                Down-votes are like kid's kisses don't reject it :-)
                Do what you want quickly because the Doomsday on 2012 :-)

                P 1 Reply Last reply
                0
                • T thatraja

                  PIEBALDconsult wrote:

                  Negative several million! Look up "SQL injection attack".

                  Yes dude it's true, But he is just expecting just correct the error. Also here before I received a rude comment from Enquirer for the same type of suggestion. Ok Here after I'll add the additional details along with answer. Thanks

                  thatraja |Chennai|India|


                  Brainbench certifications
                  Down-votes are like kid's kisses don't reject it :-)
                  Do what you want quickly because the Doomsday on 2012 :-)

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

                  Never ever ever etc. suggest concatenation. Not once, not even a little bit.

                  T 1 Reply Last reply
                  0
                  • T thatraja

                    Change the line

                    string queryString = "SELECT Name FROM patients WHERE Name LIKE '%nameSearch%';";

                    into

                    string queryString = "SELECT Name FROM patients WHERE Name LIKE '%" + nameSearch + "%'";

                    Use Code block instead of inline code for your code while posting here [Edit]As Piebal's suggestion, Use parameters with command object Click here[^][/Edit]

                    thatraja |Chennai|India|


                    Brainbench certifications
                    Down-votes are like kid's kisses don't reject it :-)
                    Do what you want quickly because the Doomsday on 2012 :-)

                    modified on Thursday, November 25, 2010 9:50 AM

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

                    According to question, Good Answer. :)

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Never ever ever etc. suggest concatenation. Not once, not even a little bit.

                      T Offline
                      T Offline
                      thatraja
                      wrote on last edited by
                      #10

                      OK Deal :cool:

                      thatraja |Chennai|India|


                      Brainbench certifications
                      Down-votes are like kid's kisses don't reject it :-)
                      Do what you want quickly because the Doomsday on 2012 :-)

                      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