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. Retrieving VARCHAR out of MSSQL with ADO

Retrieving VARCHAR out of MSSQL with ADO

Scheduled Pinned Locked Moved Database
databasec++sql-serverjsonhelp
5 Posts 2 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
    FreeCastle
    wrote on last edited by
    #1

    Hi I am using C++ and ADO to connect and retrieve data out of a MSSQL database (2005). I have a problem when executing simple queries like "SELECT MyText From MyTable" (whereas MyText would be a VARCHAR). For executing this query I use a record set like this: RecordSet->Open(...); if (!RecordSet->EndOfFile) { _bstr_t result = RecordSet->Fields->GetItem(_T("MyText"))->GetValue(); ... } After this, the result variable contains in fact a value for MyText in this table. But the problem is that it does not only contain the text of MyText but also many whitespaces behind. That means, that if MyText was e.g. a VARCHAR(100), I get the text of it and behind a sequence of spaces till my _bstr_t gets the length of 100. Is there any solution to this problem, except for parsing out the spaces behind my string by hand?

    L 1 Reply Last reply
    0
    • F FreeCastle

      Hi I am using C++ and ADO to connect and retrieve data out of a MSSQL database (2005). I have a problem when executing simple queries like "SELECT MyText From MyTable" (whereas MyText would be a VARCHAR). For executing this query I use a record set like this: RecordSet->Open(...); if (!RecordSet->EndOfFile) { _bstr_t result = RecordSet->Fields->GetItem(_T("MyText"))->GetValue(); ... } After this, the result variable contains in fact a value for MyText in this table. But the problem is that it does not only contain the text of MyText but also many whitespaces behind. That means, that if MyText was e.g. a VARCHAR(100), I get the text of it and behind a sequence of spaces till my _bstr_t gets the length of 100. Is there any solution to this problem, except for parsing out the spaces behind my string by hand?

      L Offline
      L Offline
      liur17
      wrote on last edited by
      #2

      Hi, I use _variant_t to get the value _variant_t tValue = pRS->GetFields()->GetItem(_T("YouFieldName"))->GetValue(); it works well, if you want to get CString type data, then: tValue.ChangeType(VT_BSTR);CString strValue = tValue.bstrVal;

      F 1 Reply Last reply
      0
      • L liur17

        Hi, I use _variant_t to get the value _variant_t tValue = pRS->GetFields()->GetItem(_T("YouFieldName"))->GetValue(); it works well, if you want to get CString type data, then: tValue.ChangeType(VT_BSTR);CString strValue = tValue.bstrVal;

        F Offline
        F Offline
        FreeCastle
        wrote on last edited by
        #3

        Hi, thanks for your reply. I did a mistake when posting the code in here. In fact I do it exactly the way you do it. But I have still this problem that there are a lot of spaces behind the "real" string. Any other ideas? I don't have a real idea.; it's just very strange...

        L 1 Reply Last reply
        0
        • F FreeCastle

          Hi, thanks for your reply. I did a mistake when posting the code in here. In fact I do it exactly the way you do it. But I have still this problem that there are a lot of spaces behind the "real" string. Any other ideas? I don't have a real idea.; it's just very strange...

          L Offline
          L Offline
          liur17
          wrote on last edited by
          #4

          Hi, It's very strange, i use mssql2000+sp4 and not found this trouble. why didn't you using strValue.trimRight() to trim the space off?

          F 1 Reply Last reply
          0
          • L liur17

            Hi, It's very strange, i use mssql2000+sp4 and not found this trouble. why didn't you using strValue.trimRight() to trim the space off?

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

            Yes that's of course a possibility, which means that I parse out the spaces behind the real string by hand. But I wondered if there wasn't any other possibility or if I made something wrong when getting this value from the database. But it seems that I made it correctly... that's just strange... Anyway thank you

            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