Jag använder C++.NET (v2), med enAccess 2003 databasProblem med att lagra och hämta bilder ur databas
aImage fältet i tblProducts är ett ActiveX fält
Jag klistrar in berörda funktioner
Från databas klass:
void Database::AddProducts(array<String^> ^images, String ^category){
this->dbConn->Open();
OleDbCommand ^dbComm = gcnew OleDbCommand();
dbComm->Connection = this->dbConn;
String ^sql;
int idtblProducts;
FileInfo ^fileinfo;
array<Byte> ^filedata;
MemoryStream ^ms;
OleDbParameter ^param;
Image ^image;
sql = "SELECT idtblCategories FROM tblCategories WHERE sCategory = '"+ category +"'";
dbComm->CommandText = sql;
int idtblCategories = (int)dbComm->ExecuteScalar();
sql = "SELECT idtblLanguages FROM tblLanguages WHERE sLanguageCode = '"+ this->currentLang +"'";
dbComm->CommandText = sql;
int idtblLanguages = (int)dbComm->ExecuteScalar();
for(int i=0;i<images->Length;i++){
fileinfo = gcnew FileInfo(images[i]);
image = Image::FromFile(images[i]);
ms = gcnew MemoryStream();
image->Save(ms,Imaging::ImageFormat::Bmp);
filedata = ms->ToArray();
ms->Close();
sql = "INSERT INTO tblProducts"
" (aImage,sProduct,tblCategories_idtblCategories)"
" VALUES('?','"+ fileinfo->Name +"',"+ idtblCategories +")";
dbComm->CommandText = sql;
param = dbComm->Parameters->Add("aImage", OleDbType::Binary);
param->Value = filedata;
param->Size = filedata->Length;
dbComm->ExecuteNonQuery();
dbComm->CommandText = "SELECT @@IDENTITY";
idtblProducts = (int)dbComm->ExecuteScalar();
sql = "INSERT INTO tblLocalizedProducts"
" (tblProducts_idtblProducts, tblLanguages_idtblLanguages, sProductLocalized)"
" VALUES("+ idtblProducts +","+ idtblLanguages +",'"+ fileinfo->Name +"')";
dbComm->CommandText = sql;
dbComm->ExecuteNonQuery();
}
this->dbConn->Close();
}
DataSet ^Database::GetProducts(String^ category){
this->dbConn->Open();
OleDbCommand ^dbComm = gcnew OleDbCommand();
dbComm->Connection = this->dbConn;
String ^sql = "SELECT lp.sProductLocalized, lp.sDescriptionLocalized, p.aImage "
"FROM tblProducts p, tblCategories c, tblLanguages l, tblLocalizedProducts lp "
"WHERE p.tblCategories_idtblCategories = c.idtblCategories"
" AND p.idtblProducts = lp.tblProducts_idtblProducts"
" AND l.idtblLanguages = lp.tblLanguages_idtblLanguages"
" AND l.sLanguageCode = '"+ this->currentLang +"'"
" AND c.sCategory = '"+ category +"'";
dbComm->CommandText = sql;
OleDbDataAdapter ^dbAdapter = gcnew OleDbDataAdapter(dbComm);
DataSet ^dsData = gcnew DataSet();
dbAdapter->Fill(dsData);
this->dbConn->Close();
return dsData;
}
Från "klient" klass
private: void MakeTabbedCategories(void){
ArrayList ^arrCategories = this->db->GetCategories();
TabPage ^tmpTab;
ListView ^tmpList;
ListViewItem ^tmpListItem;
ImageList ^tmpImageList;
DataSet^ dsProducts;
IEnumerator ^rowEnum;
DataRow ^row;
array<Byte> ^imagedata;
MemoryStream ^ms;
Image ^tmpImg;
this->tabControlMain->TabPages->Clear();
for each(String ^category in arrCategories){
tmpTab = gcnew TabPage();
tmpTab->Text = category;
tmpTab->UseVisualStyleBackColor = true;
tmpList = gcnew ListView();
tmpList->BorderStyle = BorderStyle::None;
tmpList->Location = Point(0,0);
tmpList->Size = Drawing::Size(760, 378);
tmpList->UseCompatibleStateImageBehavior = false;
tmpList->View = View::LargeIcon;
tmpImageList = gcnew ImageList();
dsProducts = this->db->GetProducts(category);
rowEnum = dsProducts->Tables[0]->Rows->GetEnumerator();
while(rowEnum->MoveNext()){
row = (DataRow^)(rowEnum->Current);
imagedata = (array<Byte>^)row["aImage"];
ms = gcnew MemoryStream(imagedata);
tmpImg = Image::FromStream(ms);
tmpImageList->Images->Add(row["sProductLocalized"]->ToString(), tmpImg);
ms->Close();
tmpListItem = gcnew ListViewItem();
tmpListItem->Text = row["sProductLocalized"]->ToString();
tmpListItem->ImageKey = row["sProductLocalized"]->ToString();
tmpList->Items->Add(tmpListItem);
}
tmpList->LargeImageList = tmpImageList;
tmpTab->Controls->Add(tmpList);
this->tabControlMain->TabPages->Add(tmpTab);
}
}
Problemet ligger på raden :
tmpImg = Image::FromStream(ms);
Kompilatorn säger att "parameter is not valid".
All hjälp är hjärtligt välkommen. Tack.