0
Answer

write image file save as longbinary

Marvin kakuru

Marvin kakuru

10y
901
1
hi there, i hope your ok. i have successfully designed a project that enables me to save, retrieve and view pdf,doc,excel and image files. unfortunately i can retrieve all other file formats, but i can not open image files can any body kindly help. below is the code i am using
 
{
  private void Retrieve()

{

DataFile.Text = label1.Text + textBox4.Text

if (DataFile.Text.Length <= 0)

{

MessageBox.Show("You need to specify the location where you want to save the file in the Data File section of this form.", Text);

return;

}

if (dataGridView2.SelectedCells.Count <= 0)

{

MessageBox.Show("You must select the file to download.", Text);

return;

}

System.Data.OleDb.OleDbConnection conn = null; // the connection to the database

System.Data.OleDb.OleDbCommand cmd = null; // the command

System.Data.OleDb.OleDbDataReader reader = null; // reads the data

System.IO.FileStream fileStream = null; // the file stream for the destination file

System.IO.BinaryWriter writer = null; // the writer used to create the destination file

int bufferSize = 1000; // the size of the buffer that is read from the database

// an written to the file

byte[] buffer = new byte[bufferSize]; // the buffer for the data being transfered from

// the database to the file

long startIndex = 0; // the start index of the data in the database

long numberOfBytes = 0; // the number of bytes read from the database

try

{

// create the connection to the database: AccessFile.Text is the full

// path to the Access file.

conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\katoto\Documents\Visual Studio 2010\Projects\Genesis Philcollins\Genesis Philcollins\bin\Debug\documents.accdb;Jet OLEDB:Database Password=Kireemba");

// create the SQL command: FileListView.SelectedItems[0].Text is the name

// of the file taken from the List View.

cmd = new System.Data.OleDb.OleDbCommand("SELECT [File] FROM [File] WHERE [documentno] = " + textBox7.Text + "", conn);

// open up the connection to the database

conn.Open();

// create the DataReader that will get the blob one buffer at a time

reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

// we advance to the first record returned. for this application we

// know that there is atleast one record because we got the file name

// from the List View

reader.Read();

// create the file stream that will save the file to DataFile.Text

fileStream = new System.IO.FileStream(DataFile.Text, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);

// create the writer from the file stream

writer = new System.IO.BinaryWriter(fileStream);

// read in file from the database one buffer at a time. when the number

// of bytes read is zero then we know that we are done.

do

{

numberOfBytes = reader.GetBytes(0, startIndex, buffer, 0, bufferSize);

if (numberOfBytes == 0)

{

break;

}

writer.Write(buffer, 0, (int)numberOfBytes);

startIndex += numberOfBytes;

} while (true);

writer.Flush();

}

catch (Exception ex)

{

MessageBox.Show("Could not download the file." + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.StackTrace, Text);

}

finally

{

if (writer != null)

{

writer.Close();

}

if (fileStream != null)

{

fileStream.Close();

}

if (reader != null)

{

reader.Close();

}

if (conn != null)

{

conn.Close();

}

}

System.Diagnostics.Process.Start(DataFile.Text);

button3.Visible = true;

}

any help will be highly appreciated.