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 and loops

SQL and loops

Scheduled Pinned Locked Moved C#
databasehelp
17 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
    mhmo
    wrote on last edited by
    #1

    Hi I have a table (named information) that contains columns with the names (1,2,3,4,5,6,7,8,...210) I entered a loop from i=1 to i=210 and inside the loop I write the following SQL statement: string an = "SELECT " + i.ToString() + " FROM information WHERE id = '" + s.Id + "';"; I should get the string contained in the column named i but what I get is i itself:(:( Whay would be the problem

    C J 2 Replies Last reply
    0
    • M mhmo

      Hi I have a table (named information) that contains columns with the names (1,2,3,4,5,6,7,8,...210) I entered a loop from i=1 to i=210 and inside the loop I write the following SQL statement: string an = "SELECT " + i.ToString() + " FROM information WHERE id = '" + s.Id + "';"; I should get the string contained in the column named i but what I get is i itself:(:( Whay would be the problem

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

      Well, you're asking it to select 1 from information where ID = s.ID. What did you expect ? Your columns are NAMED 1,2,3,4,5,6, up to 210 ? Sounds like God is punishing you for a terrible design.... Try this: "SELECT i." + i.ToString() + " FROM information i WHERE id = '" + s.Id + "';"; Although I don't get why you need to kill performance by making 210 SQL calls instead of one ? Christian Graus - Microsoft MVP - C++

      M 1 Reply Last reply
      0
      • C Christian Graus

        Well, you're asking it to select 1 from information where ID = s.ID. What did you expect ? Your columns are NAMED 1,2,3,4,5,6, up to 210 ? Sounds like God is punishing you for a terrible design.... Try this: "SELECT i." + i.ToString() + " FROM information i WHERE id = '" + s.Id + "';"; Although I don't get why you need to kill performance by making 210 SQL calls instead of one ? Christian Graus - Microsoft MVP - C++

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

        It did not work. exception error said: invalid use of "." in SQL statement!!!

        C 1 Reply Last reply
        0
        • M mhmo

          It did not work. exception error said: invalid use of "." in SQL statement!!!

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

          Still because of the stupid column names. Try [i." + i.ToString() "] etc. If you put the parameter in [], it may force it to accept it. Christian Graus - Microsoft MVP - C++

          M 1 Reply Last reply
          0
          • C Christian Graus

            Still because of the stupid column names. Try [i." + i.ToString() "] etc. If you put the parameter in [], it may force it to accept it. Christian Graus - Microsoft MVP - C++

            M Offline
            M Offline
            mhmo
            wrote on last edited by
            #5

            Thanks it worked :) it is not stupid :/ I am storing the answers of student each answer in one column as my istructor said. But, would u tell me the idea of ur solution and why mine is wrong?

            C 1 Reply Last reply
            0
            • M mhmo

              Thanks it worked :) it is not stupid :/ I am storing the answers of student each answer in one column as my istructor said. But, would u tell me the idea of ur solution and why mine is wrong?

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

              mhmo wrote: I am storing the answers of student each answer in one column as my istructor said. OK - that's fine, if that's how you want to do it. I just wouldn't name them 1,2,3,4,5. I'd name them something so that anyone who read the database would know what they were. Personally, I would not store them in this format at all, unless you want to use SQL to tell you who got what answer right, but that would be hard unless the questions are maths or multiple choice. mhmo wrote: But, would u tell me the idea of ur solution and why mine is wrong? Yours is wrong because it makes a round trip to the database for every answer. If you do a SELECT * from information where blah blah, then you can get back a dataRow which you can access locally to get the individual values, without making a round trip to the server every time. Christian Graus - Microsoft MVP - C++

              M 1 Reply Last reply
              0
              • C Christian Graus

                mhmo wrote: I am storing the answers of student each answer in one column as my istructor said. OK - that's fine, if that's how you want to do it. I just wouldn't name them 1,2,3,4,5. I'd name them something so that anyone who read the database would know what they were. Personally, I would not store them in this format at all, unless you want to use SQL to tell you who got what answer right, but that would be hard unless the questions are maths or multiple choice. mhmo wrote: But, would u tell me the idea of ur solution and why mine is wrong? Yours is wrong because it makes a round trip to the database for every answer. If you do a SELECT * from information where blah blah, then you can get back a dataRow which you can access locally to get the individual values, without making a round trip to the server every time. Christian Graus - Microsoft MVP - C++

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

                it will enhance the performance if I used the * format? //The answers are mulipchoice

                C 1 Reply Last reply
                0
                • M mhmo

                  it will enhance the performance if I used the * format? //The answers are mulipchoice

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

                  By a factor of 210. Instead of 210 calls to the database, you make one, and then get all the values back into a local object that you can step through to find the replies. If the questions are multiple choice, and there are always 210 questions, then there are reasons to put an answer per column, in terms of being able to query the data. I'd just name them Answer1, Answer2 or something, so I didn't need to use [] all the time. Actually, even then I think I'd be more inclined to do a table that contains the student ID, the question ID and the answer in three columns, which would allow me to have as many or as few questions as I liked, still search the database for answers, and not waste any space. Christian Graus - Microsoft MVP - C++

                  M 1 Reply Last reply
                  0
                  • C Christian Graus

                    By a factor of 210. Instead of 210 calls to the database, you make one, and then get all the values back into a local object that you can step through to find the replies. If the questions are multiple choice, and there are always 210 questions, then there are reasons to put an answer per column, in terms of being able to query the data. I'd just name them Answer1, Answer2 or something, so I didn't need to use [] all the time. Actually, even then I think I'd be more inclined to do a table that contains the student ID, the question ID and the answer in three columns, which would allow me to have as many or as few questions as I liked, still search the database for answers, and not waste any space. Christian Graus - Microsoft MVP - C++

                    M Offline
                    M Offline
                    mhmo
                    wrote on last edited by
                    #9

                    The idea is that we will use Crystal Reports to print the answers of each or a specific student in a specific format. But why we should use []?!!

                    C 1 Reply Last reply
                    0
                    • M mhmo

                      The idea is that we will use Crystal Reports to print the answers of each or a specific student in a specific format. But why we should use []?!!

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

                      mhmo wrote: The idea is that we will use Crystal Reports to print the answers of each or a specific student in a specific format. OK - either way, I think the data format I've specified is better, but I'm not familiar with Crystal Reports. If it needs all the values to be in one row, that would just be crap though. mhmo wrote: But why we should use []?!! Because a number on it's own is a reserved value, it's not really an acceptable column name. The [] tells it we used bad column names, and asks it to live with it. Christian Graus - Microsoft MVP - C++

                      M M 2 Replies Last reply
                      0
                      • M mhmo

                        Hi I have a table (named information) that contains columns with the names (1,2,3,4,5,6,7,8,...210) I entered a loop from i=1 to i=210 and inside the loop I write the following SQL statement: string an = "SELECT " + i.ToString() + " FROM information WHERE id = '" + s.Id + "';"; I should get the string contained in the column named i but what I get is i itself:(:( Whay would be the problem

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

                        Just a hint: Read up on how others have structured a multi-choice test in a database. Your table schema is plain crazy!

                        M 1 Reply Last reply
                        0
                        • C Christian Graus

                          mhmo wrote: The idea is that we will use Crystal Reports to print the answers of each or a specific student in a specific format. OK - either way, I think the data format I've specified is better, but I'm not familiar with Crystal Reports. If it needs all the values to be in one row, that would just be crap though. mhmo wrote: But why we should use []?!! Because a number on it's own is a reserved value, it's not really an acceptable column name. The [] tells it we used bad column names, and asks it to live with it. Christian Graus - Microsoft MVP - C++

                          M Offline
                          M Offline
                          mhmo
                          wrote on last edited by
                          #12

                          thanks

                          1 Reply Last reply
                          0
                          • J J4amieC

                            Just a hint: Read up on how others have structured a multi-choice test in a database. Your table schema is plain crazy!

                            M Offline
                            M Offline
                            mhmo
                            wrote on last edited by
                            #13

                            Ok Would u give a link for some thanks

                            J M 2 Replies Last reply
                            0
                            • M mhmo

                              Ok Would u give a link for some thanks

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

                              Google is your friend![^] ;)

                              1 Reply Last reply
                              0
                              • C Christian Graus

                                mhmo wrote: The idea is that we will use Crystal Reports to print the answers of each or a specific student in a specific format. OK - either way, I think the data format I've specified is better, but I'm not familiar with Crystal Reports. If it needs all the values to be in one row, that would just be crap though. mhmo wrote: But why we should use []?!! Because a number on it's own is a reserved value, it's not really an acceptable column name. The [] tells it we used bad column names, and asks it to live with it. Christian Graus - Microsoft MVP - C++

                                M Offline
                                M Offline
                                MicrosoftBob
                                wrote on last edited by
                                #15

                                >... but I'm not familiar with Crystal Reports. If it needs all the values to be in one row, that would just be crap though. You can pivot data in Crystal, but it's still crap.

                                1 Reply Last reply
                                0
                                • M mhmo

                                  Ok Would u give a link for some thanks

                                  M Offline
                                  M Offline
                                  Matt Gerrans
                                  wrote on last edited by
                                  #16

                                  Did you manually type in every column name? :doh: What is the deal with schools these days? Do they just tell you to use a database for something, without teaching you anything at all about how to use a database, or how to design your tables, etc.? Matt Gerrans

                                  M 1 Reply Last reply
                                  0
                                  • M Matt Gerrans

                                    Did you manually type in every column name? :doh: What is the deal with schools these days? Do they just tell you to use a database for something, without teaching you anything at all about how to use a database, or how to design your tables, etc.? Matt Gerrans

                                    M Offline
                                    M Offline
                                    mhmo
                                    wrote on last edited by
                                    #17

                                    No :laugh: I added them by a loop. Regarding teaching ... this is our way to learn do projects and projects projects projects projects projects projects . :omg::omg::omg:

                                    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