简体   繁体   中英

Is it possible to store .exe or .scr files in sqlserver database using asp.net c#?

I am developing an asp.net 3.5 website , I need to store some screensaver files .scr on the host machine , I wonder if there is a way to store .scr or .exe files in sqlserver database and retreive them ? any helps whould be appriciated .

Store them as BLOB (Binary Large Object) and store the file stream, which basically is a byte array. Normally the file is stored on a server and the database only holds the file path and name. You will have to make space for a BLOB data type in your database in SQL server will allow images of 8000 bytes: BLOBData varBinary(8000)

// Using DataTable as a mockup database.
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("file name");
table.Columns.Add("data");

// Read data from file.
byte[] stream = System.IO.File.ReadAllBytes("th.exe");
// Add the file to the db, don't know how you add data to your database.
table.Rows.Add("th.exe", stream);
// Create a filestream that will write data to disk (in a file).
System.IO.FileStream save = new System.IO.FileStream((string)table.Rows[0].ItemArray[0], System.IO.FileMode.Create);
// Retrieve the data from the database, don't know how you do this with your database.
byte[] data = (byte[])table.Rows[0].ItemArray[0];
// Write data to the file on disk.
save.Write(data, 0, data.Length);

Though, you can store it as BLOB, performance could be a concern if the file size is bigger.

Alternatively, you can use Filestream Storage which is implemented as a varbinary(max) column in which the data is stored as BLOBs in the file system. The sizes of the BLOBs are limited only by the volume size of the file system. The standard varbinary(max) limitation of 2-GB file sizes does not apply to BLOBs that are stored in the file system.

For more details check msdn

http://msdn.microsoft.com/en-us/library/bb933993.aspx

cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM