assigning a string, with embedded nulls, to an ADO variant_t parm

L

localharbor

I am a newbie to c++, so maybe this one's too easy, tho apparently not
for me:

1. I am using MS Visual Studio.

2. I have a std::string variable.

3. Its value might contain embedded null chars.

4. I want to insert/update it in an ADO parameter to be presented to
an SQL statement, one whose CommandText is something such as:

Select * from mytable where key = ?

So my q is, how do I convert it from its current string format into
something that ADO (i.,e. Command>-Parameter->Item[n]->Value will
accept?

I can't assign it directly, that doesn't even compile. I can't use
c_str() or any other function that employs a char * argument because
of the embedded nulls in the data, and when I try to use an
intermediate _bstr_t wrapper, the data seems to turn up garbled (can I
assume that's a single- vs double- byte problem?)

Anyway, thx if anyone can help; hope this is the correct group for
such a q.
 
A

Alf P. Steinbach

* (e-mail address removed):
I am a newbie to c++, so maybe this one's too easy, tho apparently not
for me:

1. I am using MS Visual Studio.

2. I have a std::string variable.

3. Its value might contain embedded null chars.

4. I want to insert/update it in an ADO parameter to be presented to
an SQL statement, one whose CommandText is something such as:

Select * from mytable where key = ?

So my q is, how do I convert it from its current string format into
something that ADO (i.,e. Command>-Parameter->Item[n]->Value will
accept?

I can't assign it directly, that doesn't even compile. I can't use
c_str() or any other function that employs a char * argument because
of the embedded nulls in the data, and when I try to use an
intermediate _bstr_t wrapper, the data seems to turn up garbled (can I
assume that's a single- vs double- byte problem?)

Anyway, thx if anyone can help; hope this is the correct group for
such a q.

Not really. Your question is about what characters you can use in an
SQL query. That does not include null-bytes, and it has nothing to do
with C++ or any programming language.
 
L

localharbor

* (e-mail address removed):


I am a newbie to c++, so maybe this one's too easy, tho apparently not
for me:
1. I am using MS Visual Studio.
2. I have a std::string variable.
3. Its value might contain embedded null chars.
4. I want to insert/update it in an ADO parameter to be presented to
an SQL statement, one whose CommandText is something such as:
Select * from mytable where key = ?
So my q is, how do I convert it from its current string format into
something that ADO (i.,e. Command>-Parameter->Item[n]->Value will
accept?
I can't assign it directly, that doesn't even compile. I can't use
c_str() or any other function that employs a char * argument because
of the embedded nulls in the data, and when I try to use an
intermediate _bstr_t wrapper, the data seems to turn up garbled (can I
assume that's a single- vs double- byte problem?)
Anyway, thx if anyone can help; hope this is the correct group for
such a q.

Not really. Your question is about what characters you can use in an
SQL query. That does not include null-bytes, and it has nothing to do
with C++ or any programming language.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


"Your question is about what characters you can use in an
SQL query. That does not include null-bytes, and it has nothing to do
with C++ or any programming language. "

???

Sure it does. A char value can include any sequence of characters,
including nulls, which is why I have inherited this problem. A char
value, as typified by this case, is merely a sequence of bytes, each
of which can have any bit sequence whatsoever. This is why the
"string" class itself allows embedded nulls in the first place.

Any ideas, then, as to how to solve the orig prob, to convince a
_variant_t (which is what the ADO Parameter type is, if I'm
understanding it correctly) to accept a var, of type string, with a
value that incl's null?
 
J

Jim Langston

(e-mail address removed)> wrote in message
* (e-mail address removed):


I am a newbie to c++, so maybe this one's too easy, tho apparently not
for me:
1. I am using MS Visual Studio.
2. I have a std::string variable.
3. Its value might contain embedded null chars.
4. I want to insert/update it in an ADO parameter to be presented to
an SQL statement, one whose CommandText is something such as:
Select * from mytable where key = ?
So my q is, how do I convert it from its current string format into
something that ADO (i.,e. Command>-Parameter->Item[n]->Value will
accept?
I can't assign it directly, that doesn't even compile. I can't use
c_str() or any other function that employs a char * argument because
of the embedded nulls in the data, and when I try to use an
intermediate _bstr_t wrapper, the data seems to turn up garbled (can I
assume that's a single- vs double- byte problem?)
Anyway, thx if anyone can help; hope this is the correct group for
such a q.

Not really. Your question is about what characters you can use in an
SQL query. That does not include null-bytes, and it has nothing to do
with C++ or any programming language.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


"Your question is about what characters you can use in an
SQL query. That does not include null-bytes, and it has nothing to do
with C++ or any programming language. "

???

Sure it does. A char value can include any sequence of characters,
including nulls, which is why I have inherited this problem. A char
value, as typified by this case, is merely a sequence of bytes, each
of which can have any bit sequence whatsoever. This is why the
"string" class itself allows embedded nulls in the first place.

Any ideas, then, as to how to solve the orig prob, to convince a
_variant_t (which is what the ADO Parameter type is, if I'm
understanding it correctly) to accept a var, of type string, with a
value that incl's null?

Again, it's a SQL problem. You'll have to look at _variant_t and what it
expects it's input to be and how it knows where the end of the data is.
Read the documentation on _variant_t and see what it wants to see if there's
a null in the input string. Perhaps it wants it to be escaped or something.
A SQL newsgroup is probably a better place for this question.
 
L

localharbor

"A SQL newsgroup is probably a better place for this question."

Possibly; I hadn't thought of it in those terms.

As to reading up about _variant_t, I have, and the prob is not that it
can't hold such a value, at least as near as I can tell, but that I
can't see how to assign it such a value in the first place. That would
be a good first step in determining how well it can or can't handle
it, but I haven't got past that first step yet and ... well, and here
we are.

My prob is taken from an existing system, btw, one that already
handles it (using VFP, as it happens), incl'ing the binary char data
and everything, and it works just fine, and now we want it to work
using C++/ADO, too, on a variety of databases.
 
J

Jim Langston

"A SQL newsgroup is probably a better place for this question."

Possibly; I hadn't thought of it in those terms.

As to reading up about _variant_t, I have, and the prob is not that it
can't hold such a value, at least as near as I can tell, but that I
can't see how to assign it such a value in the first place. That would
be a good first step in determining how well it can or can't handle
it, but I haven't got past that first step yet and ... well, and here
we are.

My prob is taken from an existing system, btw, one that already
handles it (using VFP, as it happens), incl'ing the binary char data
and everything, and it works just fine, and now we want it to work
using C++/ADO, too, on a variety of databases.

Okay, looking at _variant_t it is a microsoft class. Try a microsoft
newsgroup maybe.
 
B

BobR

I am a newbie to c++, so maybe this one's too easy, tho apparently not
for me:

1. I am using MS Visual Studio.

So sorry. You could fix that. (GCC MinGW)
2. I have a std::string variable.
3. Its value might contain embedded null chars.

#include <iostream>
#include <string>
#include <vector>

void CharDummy( char const *in, size_t sz ){
// do stuff with array 'in'
}

int main(){
std::string LongLineGoneBad(
"Say hello to the problem\0 in this line.");
std::cout<<"LongLineGoneBad.size()="
<<LongLineGoneBad.size()<<std::endl;
std::cout<<"LongLineGoneBad="<<LongLineGoneBad<<std::endl;

char const LongLineArray[]=("Say hello to the problem\0 in this line.");
std::cout<<"sizeof(LongLineArray)="
<<sizeof(LongLineArray)<<std::endl;

std::vector<char> vLine( LongLineArray,
LongLineArray + sizeof(LongLineArray) );
std::cout<<"vLine.size()="<<vLine.size()<<std::endl;

CharDummy( &vLine.at(0), vLine.size() );

return 0;
} // main()
/* -output-
LongLineGoneBad.size()=24
LongLineGoneBad=Say hello to the problem
sizeof(LongLineArray)=40
vLine.size()=40
*/

Try it and see if you get the same results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top