Sunteți pe pagina 1din 3

string filePath = @"D:\\Movies\sample.avi";//get the file name here FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.

Read);

BinaryReader reader = new BinaryReader(fs);

byte[] BlobValue = reader.ReadBytes((int)fs.Length); fs.Close(); reader.Close(); SqlConnection BlobsDatabaseConn = new SqlConnection("Data Source = .; Initial Catalog = BlobsDatabase; Integrated Security = SSPI");SqlCommand SaveBlobeCommand = new SqlCommand(); SaveBlobeCommand.Connection = BlobsDatabaseConn; SaveBlobeCommand.CommandType = CommandType.Text; SaveBlobeCommand.CommandText = "INSERT INTO BlobsTable(BlobFileName, BlobFile)" + "VALUES (@BlobFileName, @BlobFile)"; SqlParameter BlobFileNameParam = new SqlParameter("@BlobFileName", SqlDbType.NChar); SqlParameter BlobFileParam = new SqlParameter("@BlobFile", SqlDbType.Binary); SaveBlobeCommand.Parameters.Add(BlobFileNameParam); SaveBlobeCommand.Parameters.Add(BlobFileParam); BlobFileNameParam.Value = filePath.Substring(filePath.LastIndexOf("\\") + 1); BlobFileParam.Value = BlobValue; try { SaveBlobeCommand.Connection.Open(); SaveBlobeCommand.ExecuteNonQuery(); MessageBox.Show(BlobFileNameParam.Value.ToString() + " saved to database.","BLOB Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);

} catch(Exception ex) { MessageBox.Show(ex.Message, "Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { SaveBlobeCommand.Connection.Close(); } Retrieve the video from the server... string SavePath = @"D:\\DownMovies";//save in this path SqlConnection SaveConn = new SqlConnection("Data Source = .; Initial Catalog = BlobsDatabase; Integrated Security = SSPI"); SqlCommand SaveCommand = new SqlCommand(); SaveCommand.CommandText = "Select BlobFileName, BlobFile from BlobsTable where BlobFileName = @BlobFileName"; SaveCommand.Connection = SaveConn; SaveCommand.Parameters.Add("@BlobFileName", SqlDbType.NVarChar).Value = "My Movie.wmv"; long CurrentIndex = 0; int BufferSize = 100; long BytesReturned; byte[] Blob = new byte[BufferSize]; SaveCommand.Connection.Open(); SqlDataReader reader = SaveCommand.ExecuteReader(CommandBehavior.SequentialAccess); while (reader.Read()) { FileStream fs = new FileStream(SavePath + "\\" + reader["BlobFileName"].ToString(), FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter writer = new BinaryWriter(fs);

CurrentIndex = 0; BytesReturned = reader.GetBytes(1, //the BlobsTable column indexCurrentIndex, // the current index of the field from which to begin the read operationBlob, // Array name to write tha buffer to0, // the start index of the array to start the write operationBufferSize // the maximum length to copy into the buffer); while (BytesReturned == BufferSize) { writer.Write(Blob); writer.Flush(); CurrentIndex += BufferSize; BytesReturned = reader.GetBytes(1, CurrentIndex, Blob, 0, BufferSize); } writer.Write(Blob, 0, (int)BytesReturned); writer.Flush(); writer.Close(); fs.Close(); } reader.Close(); SaveCommand.Connection.Close();

S-ar putea să vă placă și