This does not work:
MyStream is a MemoryStream
InsertString = String::Format ("insert into blobtest (blobtest) values ('{0}');", MyStream->GetBuffer());
MyStream->GetBuffer() just returns "System.Byte[]", not the actual data, which is what I want. You can see what I'm trying to accomplish here. Why isn't it working

MemoryStream
musicandstamps
Before I answer this question, I'd like to know how you obtained MemoryStream (did you make it from a file Which file Did the user type it in )
What is the column type for blobtest
Samit
You're right. I hadn't thought about using Base64. Although it uses more storage, the added security (the base64 character set cannot be used to craft an SQL injection statement) makes it worth it (and it means DJO will not have to make code changes to his DB layer).
modtran
I have a feeling that the column type of blobtest is not of type varbinary or image. Is it of type varchar()/text
Mirko Messori
We forgot to open that connection.
String ^YourConnectionString = "Driver={SQL Native Client};Server=Aron1;Database=pubs;UID=sa;PWD=asdasd;"; // change as necessary
OdbcConnection ^DbConnection = gcnew OdbcConnection(YourConnectionString);
OdbcCommand ^odbcCom = gcnew OdbcCommand(InsertString, DbConnection);
...
odbcCom->Open();
...
delete DbConnection;
delete odbcCom;
netwanderer
It is a MySQL blob type, which is the same as a very large varbinary
When I enclose the @PicImage in quotes like this:
InsertString =
"insert into blobtest (blobtest) values ('@PicImage');";it stores "@PicImage" in the database. When I put the line:
MessageBox::Show (Mycmd->CommandText);
After
OdbcParameter ^ MyParam = Mycmd->Parameters->AddWithValue (
"@PicImage", MyStream->GetBuffer());I get the string shown above, not a string filled with a bunch of jpeg gobbledygook like I'd expect. It seems that @PicImage is not being replaced by the data from MyStream->GetBuffer()
Trevieran
DbConnection is opened and closed elsewhere.
I connect to the database just fine. It's just that the data isn't making it there. Before your suggestions, what was stored in the database was "System.Byte[]". Now it's NULL.
Scooter!
Now instead of adding "System.Byte[]" to the database, I get NULL. Here is all the C++ code:
Bitmap ^ MyImage;
gcnew Bitmap(pictureBox1->Image);MyImage =
MemoryStream ^ MyStream = gcnew MemoryStream;
MyImage->Save (MyStream, System::Drawing::Imaging::ImageFormat::Jpeg);
String ^ InsertString;
"insert into blobtest (blobtest) values (@PicImage);";InsertString =
OdbcCommand ^ Mycmd =
gcnew OdbcCommand (InsertString);Mycmd->Connection = DbConnection;
OdbcParameter ^ MyParam = Mycmd->Parameters->AddWithValue (
"@PicImage", MyStream->GetBuffer());MyParam->Direction = ParameterDirection::Input;
try{
Mycmd->ExecuteNonQuery();
}
catch (OdbcException ^ E)
{
And so on......
Mr. Paoli
redspider
Okay, the syntax for MySQL parameters is slightly different to SQL Server. If you want to know how to work with parameters in MySQL, I suggest contacting them for support (a quick search indicates it has something to do with replacing @ with ).
zaynun hammoud
Assumption (most probably correct): You are trying to insert a bitmap image into an SQL server database.
You'll probably want to change the way you build InsertString. It is susceptible to a injection attack. Granted, the flaw is heavily mitigated due to the fact that it comes from a picture, but it is still possible to crash the SQL command if I create a specially crafted picture. In any case I'd be a lot happier if you used a parametrised query instead:
Nicholas T
You can also convert the byte array to Base64 before sending it to the database!
The resultant base64 string is almost three times bigger than the raw byte array but allows you to manage binary data in a friendly way for SOAP, HTTP, SMTP, SQL Queries (like yours) and every full text compatible protocol and language.
You can do the following:
String* b64string = Convert::ToBase64String(bytearray);
//where bytearray is your array of bytes that comprise your bitmap.
//The reverse operation is:
System::Byte bytearray[] = Convert::FromBase64String(b64string);
=)
Marek Zgadzaj
MyImage = gcnew Bitmap(pictureBox1->Image);
MemoryStream ^ MyStream = gcnew MemoryStream;
MyImage->Save (MyStream, System::Drawing::Imaging::ImageFormat::Jpeg);
String ^ InsertString;
InsertString = String::Format ("insert into blobtest (blobtest) values ('{0}');", MyStream->GetBuffer());