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. SQL or query

SQL or query

Scheduled Pinned Locked Moved C#
databasequestion
9 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.
  • F Offline
    F Offline
    falles01
    wrote on last edited by
    #1

    Hi again, Now I have been told I have to finish this by Monday. What a boss ha. They told me to not worry about sql innjection attacks at the moment because thats not what they are judging me on. This is what I am trying to do :-D string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "'"; string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "' select * from Role where RoleID = '" + rolecomboBox.SelectedValue.ToString() + "')"; the employees part searches, but when I na from the employee drop down and want to search on role, this statment brings up nothing for role. Do I need to do an or or something similar? The one with the smiley face works, its just when I try to add the other drop down list queries it doesn't.

    M R C 3 Replies Last reply
    0
    • F falles01

      Hi again, Now I have been told I have to finish this by Monday. What a boss ha. They told me to not worry about sql innjection attacks at the moment because thats not what they are judging me on. This is what I am trying to do :-D string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "'"; string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "' select * from Role where RoleID = '" + rolecomboBox.SelectedValue.ToString() + "')"; the employees part searches, but when I na from the employee drop down and want to search on role, this statment brings up nothing for role. Do I need to do an or or something similar? The one with the smiley face works, its just when I try to add the other drop down list queries it doesn't.

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello,

      falles01 wrote:

      hey told me to not worry about sql innjection attacks at the moment because thats not what they are judging me on.

      Ok, but you really should read what Colin suggested 7hr ago: http://www.codeproject.com/script/comments/forums.asp?msg=2196907&forumid=1649#xx2196907xx[^] There you would find this nice example (uses 'AND'): // Get the valid user name and friendly name of the favourite int uid = this.GetUserID(); string friendlyName = this.GetFriendlyName(); // Create the SQL statement to retrieve the search criteria string sql = string.Format("SELECT Criteria FROM Favourites "+ "WHERE UserID={0} AND FriendlyName='{1}'", uid, friendlyName); SqlCommand cmd = new SqlCommand(sql, this.Connection); string criteria = cmd.ExecuteScalar(); // Do the search sql = string.Format("SELECT * FROM Products WHERE ProductName = '{0}'", criteria); SqlDataAdapter da = new SqlDataAdapter(sql, this.Connection); da.Fill(this.productDataSet);

      All the best, Martin

      F 2 Replies Last reply
      0
      • F falles01

        Hi again, Now I have been told I have to finish this by Monday. What a boss ha. They told me to not worry about sql innjection attacks at the moment because thats not what they are judging me on. This is what I am trying to do :-D string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "'"; string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "' select * from Role where RoleID = '" + rolecomboBox.SelectedValue.ToString() + "')"; the employees part searches, but when I na from the employee drop down and want to search on role, this statment brings up nothing for role. Do I need to do an or or something similar? The one with the smiley face works, its just when I try to add the other drop down list queries it doesn't.

        R Offline
        R Offline
        Rocky
        wrote on last edited by
        #3

        falles01 wrote:

        string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "' select * from Role where RoleID = '" + rolecomboBox.SelectedValue.ToString() + "')";

        try an put a MessageBox.Show(sql); after you make this string and see whats being made. I think there is this extra ) at the end or this SQL Query is incomplete. I Can't understand why u'r having 2 selects in one SQL Statement.

        Rocky You can't climb up a ladder with your hands in your pockets.

        1 Reply Last reply
        0
        • M Martin 0

          Hello,

          falles01 wrote:

          hey told me to not worry about sql innjection attacks at the moment because thats not what they are judging me on.

          Ok, but you really should read what Colin suggested 7hr ago: http://www.codeproject.com/script/comments/forums.asp?msg=2196907&forumid=1649#xx2196907xx[^] There you would find this nice example (uses 'AND'): // Get the valid user name and friendly name of the favourite int uid = this.GetUserID(); string friendlyName = this.GetFriendlyName(); // Create the SQL statement to retrieve the search criteria string sql = string.Format("SELECT Criteria FROM Favourites "+ "WHERE UserID={0} AND FriendlyName='{1}'", uid, friendlyName); SqlCommand cmd = new SqlCommand(sql, this.Connection); string criteria = cmd.ExecuteScalar(); // Do the search sql = string.Format("SELECT * FROM Products WHERE ProductName = '{0}'", criteria); SqlDataAdapter da = new SqlDataAdapter(sql, this.Connection); da.Fill(this.productDataSet);

          All the best, Martin

          F Offline
          F Offline
          falles01
          wrote on last edited by
          #4

          oh can I use seperate statements? Life for instance string sql = and then sql = do i have to use string.format? after sql = ? Thanks ;)

          1 Reply Last reply
          0
          • M Martin 0

            Hello,

            falles01 wrote:

            hey told me to not worry about sql innjection attacks at the moment because thats not what they are judging me on.

            Ok, but you really should read what Colin suggested 7hr ago: http://www.codeproject.com/script/comments/forums.asp?msg=2196907&forumid=1649#xx2196907xx[^] There you would find this nice example (uses 'AND'): // Get the valid user name and friendly name of the favourite int uid = this.GetUserID(); string friendlyName = this.GetFriendlyName(); // Create the SQL statement to retrieve the search criteria string sql = string.Format("SELECT Criteria FROM Favourites "+ "WHERE UserID={0} AND FriendlyName='{1}'", uid, friendlyName); SqlCommand cmd = new SqlCommand(sql, this.Connection); string criteria = cmd.ExecuteScalar(); // Do the search sql = string.Format("SELECT * FROM Products WHERE ProductName = '{0}'", criteria); SqlDataAdapter da = new SqlDataAdapter(sql, this.Connection); da.Fill(this.productDataSet);

            All the best, Martin

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

            Excellent it works using sql = Thank you very much :-D

            M 1 Reply Last reply
            0
            • F falles01

              Excellent it works using sql = Thank you very much :-D

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Hello, I'm glad I could help you help yourself. :)

              All the best, Martin

              1 Reply Last reply
              0
              • F falles01

                Hi again, Now I have been told I have to finish this by Monday. What a boss ha. They told me to not worry about sql innjection attacks at the moment because thats not what they are judging me on. This is what I am trying to do :-D string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "'"; string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "' select * from Role where RoleID = '" + rolecomboBox.SelectedValue.ToString() + "')"; the employees part searches, but when I na from the employee drop down and want to search on role, this statment brings up nothing for role. Do I need to do an or or something similar? The one with the smiley face works, its just when I try to add the other drop down list queries it doesn't.

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                This is two statements. Try running them seperately. Also, you're still passing Ids as strings, I assume this means they are strings in the DB, which is not efficient at all.

                Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                F 2 Replies Last reply
                0
                • C Christian Graus

                  This is two statements. Try running them seperately. Also, you're still passing Ids as strings, I assume this means they are strings in the DB, which is not efficient at all.

                  Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  F Offline
                  F Offline
                  falles01
                  wrote on last edited by
                  #8

                  Yes you are right. I thought the following worked because I was testing them one by one, but if I want to search on the employee (being the first statement), then I can't. I can only search on division. I am guessing I need to put it in one statement. Could you show me with the correct notation how to do that? Also I was informed of the string sql from somewhere, but what do you suggest in place of that? string sql = "select * from employees where employeeID= '" + empNamecomboBox.SelectedValue.ToString() + "' or managerID = '" + managercomboBox.SelectedValue.ToString() + "' or DivisionID = '" + divisioncomboBox.SelectedValue.ToString() + "' or RoleID = '" + rolecomboBox.SelectedValue.ToString() + "'"; sql = "select * from Role where RoleID= '" + rolecomboBox.SelectedValue.ToString() + "'"; sql = "select * from Manager where ManagerID= '" + managercomboBox.SelectedValue.ToString() + "'"; sql = "select * from Division where DivisionID= '" + divisioncomboBox.SelectedValue.ToString() + "'";

                  1 Reply Last reply
                  0
                  • C Christian Graus

                    This is two statements. Try running them seperately. Also, you're still passing Ids as strings, I assume this means they are strings in the DB, which is not efficient at all.

                    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    F Offline
                    F Offline
                    falles01
                    wrote on last edited by
                    #9

                    You were right these people are not good to work for. I am going to leave and go back to my old job. I don't need to be this stressed.

                    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