|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis is my small contribution from what I learnt in my walk towards VC++ .NET. It's a really simple and very often used scenario: working with Code ExplanationRequirements
I used SQL Server for database operations. The application shows how the values (image) are stored in the database and the grid is updated when changes are made in the database. This is the snapshot of my application. Controls used
Create the databaseWhen you click the button 'Create Database', the table called Picture would be created in the SQL Server NorthWind database. If it is already present, it would be deleted and created again. String* strSQL = S"IF EXISTS (SELECT * FROM northwind.dbo.sysobjects WHERE NAME " "= 'Picture' AND TYPE = 'u')\r\nBEGIN\r\nDROP " "TABLE northwind.dbo.picture\r\nEND\r\nCREATE TABLE " "Picture (PictureID Int IDENTITY(1,1) NOT NULL,[FileName] " "Varchar(255) NOT NULL,Picture Image NOT NULL," "CONSTRAINT [PK_Picture] PRIMARY KEY CLUSTERED(PictureID))"; Try { SqlConnection* conCreate = new SqlConnection(strconString); SqlCommand* sqlCmd = new SqlCommand(strSQL,conCreate); conCreate->Open(); sqlCmd->ExecuteNonQuery(); conCreate->Close(); MessageBox::Show(S"Table is Created Successfully",S"Table Creation",MessageBoxButtons::OK,MessageBoxIcon::Information); } catch(System::Exception* ee) { MessageBox::Show(ee->Message,S"Error",MessageBoxButtons::OK, MessageBoxIcon::Information); } Place the code to create the table and declare the appropriate variables in the program. Select ImageThe open file dialog is used to select the particular image. When the Select Image button is clicked, the open file dialog would be displayed to select the image (BMP/GIF/JPG). openFileDialog1->InitialDirectory = S"C:\\"; openFileDialog1->Filter = S"All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"; openFileDialog1->FilterIndex = 2; if(openFileDialog1->ShowDialog()== DialogResult::OK) { pictureBox1->Image = Image::FromFile(openFileDialog1->FileName); pictureBox1->SizeMode = PictureBoxSizeMode::CenterImage; pictureBox1->BorderStyle = BorderStyle::Fixed3D; lblImage_Path->Text = openFileDialog1->FileName; } Place the code in the Select Image button Save to DatabaseWhen the Save to Database button is clicked, the selected image would be stored into the Picture table by means of converting it into a stream of bytes. After storing into database, refresh the //To Convert the Image to Bytes MemoryStream* mst = new MemoryStream(); pictureBox1->Image->Save(mst,pictureBox1->Image->RawFormat); Byte BinaryImg[] = mst->GetBuffer(); mst->Close(); Then, insert the image name to the database. //Insert into Table conNwnd = new SqlConnection(strconString); String* strSQL = S"INSERT INTO Picture" " (Filename, Picture)VALUES (@Filename, @Picture)"; //To Fill the DataGrid sqlcmd = new SqlCommand(strSQL,conNwnd); sqlcmd->Parameters->Add(new SqlParameter(S"@Filename", SqlDbType::NVarChar,50))->Value = strImgFile[0]; sqlcmd->Parameters->Add(new SqlParameter(S"@Picture", SqlDbType::Image))->Value = BinaryImg; conNwnd->Open(); sqlcmd->ExecuteNonQuery(); conNwnd->Close(); RefreshTable(); MessageBox::Show(String::Concat(strImgFile[0],S" saved to Database."), S"Image Status",MessageBoxButtons::OK,MessageBoxIcon::Information); Here, RefreshTable() conNwnd = new SqlConnection(strconString); SqlDataAdapter* sqlDA = new SqlDataAdapter(S"Select PictureID,Filename," "Picture FROM Picture",conNwnd); TableData->Clear(); sqlDA->Fill(TableData,INPUT_TABLE); dataGrid1->DataSource = TableData->Tables->Item[INPUT_TABLE]; On the form Load call, this ConclusionUsing this small application, a beginner can understand the
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||