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