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 / C++ / MFC
  4. Problems again- Newbie

Problems again- Newbie

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
10 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.
  • A Offline
    A Offline
    antonaras
    wrote on last edited by
    #1

    Hi buddies i have a relativly simple question i whant to execute a select statment in c++ but for the WHERE part of the statment i want to read it from a variable of type string Let me be more precise. string token; token="SomeName" pCommand->CommandText = "SELECT * FROM Table1 WHERE Name=?";//WHERE Name=token Any ideas pls help Thanks again

    N S B H 4 Replies Last reply
    0
    • A antonaras

      Hi buddies i have a relativly simple question i whant to execute a select statment in c++ but for the WHERE part of the statment i want to read it from a variable of type string Let me be more precise. string token; token="SomeName" pCommand->CommandText = "SELECT * FROM Table1 WHERE Name=?";//WHERE Name=token Any ideas pls help Thanks again

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      Use _tcscat to concatenate the two strings.


      Nibu thomas A Developer Programming tips[^]  My site[^]

      A 1 Reply Last reply
      0
      • A antonaras

        Hi buddies i have a relativly simple question i whant to execute a select statment in c++ but for the WHERE part of the statment i want to read it from a variable of type string Let me be more precise. string token; token="SomeName" pCommand->CommandText = "SELECT * FROM Table1 WHERE Name=?";//WHERE Name=token Any ideas pls help Thanks again

        S Offline
        S Offline
        Steve S
        wrote on last edited by
        #3

        What you really need is a parameterised query. The idea is that you create a parameter, and associate it with the command object. You can then fill in the value, and run the command. Check out the documentation for CreateParameter. This is preferred to concatenating the text because it avoids problems with strings containing quotes, and the so-called SQL injection attacks. Steve S Developer for hire

        A 1 Reply Last reply
        0
        • N Nibu babu thomas

          Use _tcscat to concatenate the two strings.


          Nibu thomas A Developer Programming tips[^]  My site[^]

          A Offline
          A Offline
          antonaras
          wrote on last edited by
          #4

          Hey Nibu thanks for the reply can you give me a code sample of how to use it thanks appreciate the help

          N 1 Reply Last reply
          0
          • S Steve S

            What you really need is a parameterised query. The idea is that you create a parameter, and associate it with the command object. You can then fill in the value, and run the command. Check out the documentation for CreateParameter. This is preferred to concatenating the text because it avoids problems with strings containing quotes, and the so-called SQL injection attacks. Steve S Developer for hire

            A Offline
            A Offline
            antonaras
            wrote on last edited by
            #5

            Hey Steve allways appreciate the help (again:-D) I hope i'm not asking much but can you give me an example

            S 1 Reply Last reply
            0
            • A antonaras

              Hey Nibu thanks for the reply can you give me a code sample of how to use it thanks appreciate the help

              N Offline
              N Offline
              Nibu babu thomas
              wrote on last edited by
              #6

              antonaras wrote:

              Hey Nibu thanks for the reply can you give me a code sample of how to use it thanks appreciate the help

              Look at what steve said. That is the right way to do it.


              Nibu thomas A Developer Programming tips[^]  My site[^]

              1 Reply Last reply
              0
              • A antonaras

                Hi buddies i have a relativly simple question i whant to execute a select statment in c++ but for the WHERE part of the statment i want to read it from a variable of type string Let me be more precise. string token; token="SomeName" pCommand->CommandText = "SELECT * FROM Table1 WHERE Name=?";//WHERE Name=token Any ideas pls help Thanks again

                B Offline
                B Offline
                BadKarma
                wrote on last edited by
                #7

                Try to use a temp string for the query

                string token;
                token = "SomeName";
                
                string query;
                query = "SELECT * FROM Table1 WHERE Name=" + token;
                
                pCommand->CommandText = query.data()
                

                codito ergo sum

                A 1 Reply Last reply
                0
                • B BadKarma

                  Try to use a temp string for the query

                  string token;
                  token = "SomeName";
                  
                  string query;
                  query = "SELECT * FROM Table1 WHERE Name=" + token;
                  
                  pCommand->CommandText = query.data()
                  

                  codito ergo sum

                  A Offline
                  A Offline
                  antonaras
                  wrote on last edited by
                  #8

                  Hey BadKarma thanks for the reply looks like is getting there i used your code and it compiles with no prob but i get an error at runtime Error:ΘÆ Press any key to continue

                  1 Reply Last reply
                  0
                  • A antonaras

                    Hi buddies i have a relativly simple question i whant to execute a select statment in c++ but for the WHERE part of the statment i want to read it from a variable of type string Let me be more precise. string token; token="SomeName" pCommand->CommandText = "SELECT * FROM Table1 WHERE Name=?";//WHERE Name=token Any ideas pls help Thanks again

                    H Offline
                    H Offline
                    Hamid Taebi
                    wrote on last edited by
                    #9

                    strcpy(string,"SELECT * FROM Table1 WHERE Name=?"); strcat( string, token ); ------------- wsprintf(string,"SELECT * FROM Table1 WHERE Name=%s",token);_**


                    **_

                    whitesky


                    1 Reply Last reply
                    0
                    • A antonaras

                      Hey Steve allways appreciate the help (again:-D) I hope i'm not asking much but can you give me an example

                      S Offline
                      S Offline
                      Steve S
                      wrote on last edited by
                      #10

                      This isn't pretty, nor is it optimal, but it covers the basics; You'll want to reindent the code, :) and I do things a little differently, by using raw interfaces when importing. Where there is a call to doQuery, you can put so UI in there to ask for & validate a query field. This is a bit rough in places, normally I use OLE DB rather than ADO, but it works. You'll need to change the name of the data source (obviously). The trick is to create the parameter and associate it with the command object. You can then use Execute to get a recordset, and having done that, you can ask the recordset to requery without needing the command object (once you've changed the parameter value. Add a different call to doQuery with a different string below the first one, and you'll see what I mean. The advantage over straight SQL text building is that in many cases, the SQL needs to be parsed only once, which is handy for complex queries, and in addition, it protects you from some nasty so-and-so from performing a SQL injection attack. (Yes, it was asking a bit much, but I had a spare 10 minutes at lunchtime[local time]) #include <windows.h> #include <tchar.h> #include <ole2.h> #import "c:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") raw_interfaces_only #include <stdio.h> #include <conio.h> void printRows(_Recordset* pRstTemp) { VARIANT_BOOL bEOF; // Ensure at top of recordset. pRstTemp->MoveFirst(); // If EOF is true, then no data and skip print loop. pRstTemp->get_EndOfFile(&bEOF); if (bEOF) { _tprintf(_T("\tRecordset empty\n")); } else { // Define temporary strings for output conversions. // Initialize to first record's values. _bstr_t bstrTitle; _bstr_t bstrType; // Enumerate Recordset and print from each. while(!bEOF) { // Convert variant string to convertable string type. FieldsPtr spFields; FieldPtr spField; long nFields; pRstTemp->get_Fields(&spFields); spFields->get_Count(&nFields); for(long f = 0; f < nFields; f++) { BSTR t = NULL; spFields->get_Item(_variant_t(f), &spField); _variant_t v; spField->get_Value(&v); spField->get_Name(&t); if (v.vt != VT_NULL) { v.ChangeType(VT_BSTR); #ifdef _UNICODE _tprintf(_T("%20.20s: %s\n"), (LPCWSTR)t, (LPCWSTR)v.bstrVal); #else _tprintf(_T("%20.20S: %S\n"), (LPCWSTR)t, (LP

                      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