Send and Retrieve files to Database

hi again...

This time i'm stuck on an issue concerning sending and retrieving *.exe files to and from a SQL database.

Code to send:

FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.ReadWrite);

byte[] MyFile = new byte[fs.Length];

fs.Read(MyFile, 0, System.Convert.ToInt32(fs.Length));

fs.Close();

dAdapter = new SqlDataAdapter("Select * from system where id=1", cnn);

SqlCommandBuilder command = new SqlCommandBuilder(dAdapter);

DataSet ds = new DataSet("system");

dAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

dAdapter.Fill(ds, "system");

DataRow row;

row = ds.Tables["system"].NewRow();

row["id"] = "1";

row["valuebin"] = MyFile;

ds.Tables["system"].Rows.InsertAt(row, 0);

dAdapter.Update(ds, "system");

Code to retrive:

byte[] MyFile = new byte[0];

string filename;

cnn.Open();

dAdapter = new SqlDataAdapter("Select * from system where id=1", cnn);

SqlCommandBuilder comm = new SqlCommandBuilder(dAdapter);

DataSet ds = new DataSet("system");

dAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

dAdapter.Fill(ds, "system");

DataRow row;

row = ds.Tables["system"].Rows[0];

MyFile = (byte[])row["valuebin"];

filename = "MyFileName.exe";

int ArraySize = new int();

ArraySize = MyFile.GetUpperBound(0);

FileStream fs = new FileStream(Application.StartupPath + "\\" + filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);

fs.Write(MyFile, 0, ArraySize);

fs.Close();

}

}

}

Well, the problem is that I can't use the file i've retrieved. I get "the file isn't a win32 valid application" when i try to run it.

I'm using this code to store and retrive other file types, but this only happens with *.exe's.

Any ideas on why this is happening

Thanks in advance.




Answer this question

Send and Retrieve files to Database

  • Jonathan Allen

    Thank you Markku

    Can you explain me why the method i used worked on files other than executable ones Should i always use the method you told me

    Thank you so much...



  • Alberto Acerbis

    found out that it as nothing to do with the database...

    tryed:

    FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.ReadWrite);

    byte[] MyFile = new byte[fs.Length];

    fs.Read(MyFile, 0, System.Convert.ToInt32(fs.Length));

    fs.Close();

    int ArraySize = new int();

    ArraySize = MyFile.GetUpperBound(0);

    string filen = "TryMe.exe";

    FileStream write = new FileStream(Application.StartupPath + "\\" + filen, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);

    write.Write(MyFile, 0, ArraySize);

    write.Close();

    and TryMe.exe is not a valid win32 application



  • ultek

    Hi Pedro

    I'm not sure that it works OK on other files either. I testi it with text file and it lost last character.

    Actually reason of that erros is that GetUpperBound(0) give biggest index of array and size is 1 bigger than last index.

    Yours Markku

  • Webhostbudd

    Hi Pedro

    Change MyFile.GetUpperBound(0) to MyFile.Length and I think that file is valid.

    Yours

    Markku


  • Send and Retrieve files to Database