Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, this is my code :
C#
private void BtnDisplay_Click(object sender, EventArgs e)
        {
            if (GridFile.CurrentCell == null)
            {
                MessageBox.Show("select a row");
                return;
            }
            DataTable dt = new DataTable();
            int SelectedRow = GridFile.CurrentCell.RowIndex;
            string OriginalPath = GridFile.SelectedRows[0].Cells[2].Value.ToString();
            string tempsafefilename = GridFile.SelectedRows[0].Cells[5].Value.ToString();
            saveFileDialog1.FileName = OriginalPath;
            string FileName = saveFileDialog1.FileName;
            //.......
            if (File.Exists(FileName))
            {
                Process.Start(FileName);
            }
            else
            {
 
byte[] FileData = new byte[32768];
               FileData = (byte[])DS.Tables["TeacherFile"].Rows[SelectedRow]["FileData"];
                String newfile = @"D:\Temp";
                DirectoryInfo l_dDirInfo = new DirectoryInfo(newfile);
                if (l_dDirInfo.Exists == false)
                {
                    Directory.CreateDirectory(newfile);
                }
 
                string NewPath = newfile + "\\" + tempsafefilename;
 

                    //FileStream fStream = new FileStream(NewPath, FileMode.Create);
 
                    //while (fStream.Read(FileData, 0, FileData.Length) > 0)
                    //{
                    //    Process.Start(NewPath);
                    //}
                   
                    using (FileStream fStream = new FileStream(NewPath, FileMode.Create))
                    {
                        fStream.Write(FileData, 0, FileData.Length);
                        FileData = ReadToEnd(fStream);
                        fStream.Write(FileData, 0, FileData.Length);
                        Process.Start(NewPath);
                    }
 
 public static byte[] ReadToEnd(System.IO.Stream stream)
        {
            long originalPosition = 0;
 
            if (stream.CanSeek)
            {
                originalPosition = stream.Position;
                stream.Position = 0;
            }
 
            try
            {
                byte[] readBuffer = new byte[4096];
 
                int totalBytesRead = 0;
                int bytesRead;
 
                while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                {
                    totalBytesRead += bytesRead;
 
                    if (totalBytesRead == readBuffer.Length)
                    {
                        int nextByte = stream.ReadByte();
                        if (nextByte != -1)
                        {
                            byte[] temp = new byte[readBuffer.Length * 2];
                            Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                            Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                            readBuffer = temp;
                            totalBytesRead++;
                        }
                    }
                }
 

                byte[] buffer = readBuffer;
                if (readBuffer.Length != totalBytesRead)
                {
                    buffer = new byte[totalBytesRead];
                    Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                }
                return buffer;
            }
            finally
            {
                if (stream.CanSeek)
                {
                    stream.Position = originalPosition;
                }
            }
        }
        //....OKKKKKK
        byte[] ReadFile(string sPath)
        {
 

            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[32768];
            int read = 0;
            int chunk;
            fStream.Position = 3147483648;
            while ((chunk = fStream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;
 
                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = fStream.ReadByte();
 
                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        return buffer;
                    }
 
                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }

what my program does, is that convert a file (any file- movie - music - zip...) to the codes (0 and 1 - I think) and save it to my SQL database. now when I click on (open file - show) button it show me the file but the file is destroyed. the below link is a picture of "veronica movie" ( it's about 700 MB) I save it with my software and when I clicked on open, my software opened the file but it was corrupted and the KMPlayer show me those codes. how to fix it? please help me.

https://i.imgsafe.org/f52cc7409c.png[^]

What I have tried:

i put my old codes here :

http://snipsave.com/user/profile/johnnyflow#14098
Posted
Updated 20-Jul-16 0:58am
v4

1 solution

The chances are that you aren't saving the right data in the DB, or you aren't retrieving it and saving it again properly.
The easiest way to save a file in SQL is to load it directly into a byte array, and save that:
C#
byte[] data = File.readAllBytes(pathToFile);

And to write the bytes back to a file is also trivial:
C#
File.WriteAllBytes(pathToFile, data);

There is info on how to save and retrieve it to the DB here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it's based aroung images, but the principles are exactly the same for video data.

But...I wouldn't store video data in a database: it's big data and it will cause a lot of bottlenecks on your system to save and retrieve it.
Instead, store the video as a file and store the file location in the DB.
The way I'd do it is to copy the file to a "known" location with a Guid filename using File.Copy and then store the Guid-based path plus the original file name in the DB.
You can then recreate the original by using File.Copy to put it back when needed.
 
Share this answer
 
Comments
Johny Flow 20-Jul-16 8:23am    
thank u very much. can u please tell me exactly where should i write those 2 lines of codes ?!! (I'm very new to programming and English is my second language - i create that program in 1 month and it's still not working. it's just so hard). and i have another question: i want to load that file even when it's deleted or moved ( i mean i put a file to my desktop and convert it to code with my program and save it and remove that file from my desktop. now i want my program to open that file from database
OriginalGriff 20-Jul-16 8:43am    
As I said, I wouldn't store it in a DB at all - it really isn't a good idea!
But...the first line reads a file into memory. So when you want to read the file... :laugh:
You can then use the code in the link to save it to the DB.
The second is to write it to a file - so you use it after the code in the link!

As for you second question: get it working to and from the DB first - then worry about anything else. But if you are using this to "hide" you video collection for some reason, then be aware that it won't hide it for more than a few seconds from anyone halfway competent!
Johny Flow 20-Jul-16 9:04am    
i know i shouldn't store huge files in db but i'm testing something ... . no i don't want to hide movies or anything else i just want to backup files ( so when i accidentally delete a file i could recover it again ). thank u.
OriginalGriff 20-Jul-16 9:19am    
Don't write your own - it's not the best way, trust me! Instead, look at something like this:

http://www.aomeitech.com/aomei-backupper.html

I use it: the free version is excellent, so good I paid my own money for the full one!
And you really, really don't want to do backups onto the same disk: that just means when you get a disk problem you lose the backup as well! And backup to SQL DB via a network will be painful, via the internet it will be horrifically slow. To backup, use a removable USB drive and disconnect it when you are finished (so that ransomware can't corrupt your backups as well).
Johny Flow 20-Jul-16 12:45pm    
Ok, i'll use aomei backupper. i know i have 2 external hdd ( 1. 500gb - 2. 2TB ) and i backup all my files to hdd (2TB) about every week because my laptop hdd was erased before and i lost about 600 GBs of information and i wanted to punch the laptop but u know it's expensive :D ! ( so i bought a 2tb external hdd and ... ). one last question ( i promise :) was my english good?!. thanks man for everything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900