Monday, March 26, 2012

retrive a file from a binary data field

hello dear friends

I am trying to save a binary file to database with the following code:

publicstaticvoid memorystreamToDb()

{

MemoryStream mst =newMemoryStream(); UnicodeEncoding u =newUnicodeEncoding(); string Textn ="Test"; byte[] b = u.GetBytes(Textn);

mst.Write(b ,0,Textn.Length );

BinaryReader reader =newBinaryReader(mst); byte[] file = reader.ReadBytes((int)mst.Length); using (SqlConnection connection =newSqlConnection("Some Connection String"))

{

SqlCommand command =newSqlCommand("INSERT INTO temp (examplefile) Values(@.File)", connection);

command.Parameters.Add(

"@.File",SqlDbType.Binary, file.Length).Value = file;

connection.Open();

command.ExecuteNonQuery();

}

reader.Close();

mst.Close();

}

or with the other method from a real file (not memory stream)

publicstaticvoid Addfile(string path)

{

CommonMethods_class k =newCommonMethods_class(); byte[] file = GetFile(path); using (SqlConnection connection =newSqlConnection(k.Get_connection_string()))

{

SqlCommand command =newSqlCommand("INSERT INTO temp (examplefile) Values(@.File)", connection);

command.Parameters.Add(

"@.File",SqlDbType.Binary, file.Length).Value = file;

connection.Open();

command.ExecuteNonQuery();

}

}

and after running each of them seams that the file have been saved; but I can not retrive the files. I have tried some solutions from msdn but inside the created file is empty. the point that i really look for it is to just working with memory not in the disk before saving. and then retriving each field that I want.

looking forward your points

thank you in advance

Hiashk1860 ,

If you haven't readRead and Write BLOB Data by Using ADO.NET Through ASP.NET you can take a look.

|||

hi

thank you for yor response, Yes, I have read the article and some other Articles and still have problem.

No comments:

Post a Comment