Store and retrieve VSD file into SQL FILETABLE - c#

How to save a .VSD file into a FileTable in SQL Server 2012? And also need to retrieve and display the .VSD file on a web page?
Filetable column : file_stream varbinary(max)
Any help? Thank you.
Code used:
sfileName = "sample.vsd"
Dim fs As New FileStream(sfileName, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(CInt(fs.Length))

VARBINARY(MAX) would be the correct data type to store binary data and you will want to use the FILESTREAM attribute. If you assume that this is the only type of file you will be storing in your table, then you are good. If you need to store multiple types of files you will want to add another column to capture the file type and then have your application display it correctly.
Check out the following post that should give you a better understanding. Article mentions SQL 2008 but it is applicable to 2012 and 2014:
http://blogs.technet.com/b/dataplatforminsider/archive/2008/04/14/sql-server-2008-makes-it-easy-to-manage-blobs-and-files.aspx

I have not found a way to save and retrieve the Visio (.vsd) file from the FILETABLE in SQL server 2012.
I tried to save the Visio file to the latest version of Visio (.vsdx) but it didn't work.
The work around for it is save the .vsd file to PDF or Image file. Then save the file to SQL Server FILETABLE using the code mentioned in my question.

Related

Concatenate two PDF files saved as a blob

I am creating a web app. For this purpose I use Microsoft SQL Server 2016 (SP1-GDR) (KB4505219) - 13.0.4259.0 (X64) and ASP.NET MVC 5.
In my database I have a table with PDF files in it. I have stored the file content as blob in that table. I have also stored the filename in that table.
Now I want to give my clients the possibility to download multiple PDF files. The PDF files should be concatenated. So if the client requests for all PDF files, he or she should get all PDF files concatenated to one single PDF file.
I thought about two ways to concatenate the PDF files.
First is to concatenate the PDF files in the SQL Server query. So when I get the blob from the database it is already concatenated and I do not have to do anything further in my C# code.
Second is to get all blobs from the database and concatenate the blobs in C#.
I have tried to use the COALESCE function in SQL Server to concatenate the blobs in SQL Server.
DECLARE #blobcontent varbinary(max);
SELECT
#blobcontent = COALESCE(#blobcontent, 0x0) + FileTable.fileContent
FROM
FileTable;
SELECT #blobcontent;
This did not work for me.
So then I tried to concatenate the blob in C# after getting all blobs from the database. I tried to create a new byte-array variable and put all blobs together into that new byte-array.
...
var file1 = GetFileById(1); /*the function GetFileById(int id) returns a tuple (byte[], string) with the file-content as the first Element and the file-name as the second element*/
var file2 = GetFileById(2);
var concatenatedFile = file1.Item1.Concat(file2.Item1).ToArray();
...
This did also not work for me.
Is there a smart way to solve this problem?
Ok. As I also suspected, it is not possible to concatenate blobs in this form (see comment by PSK). I will solve this problem by simply providing the client the files as zip, because I don't want to include any other libraries for this.

How to store a zip file in sql server table

I have a problem and I do not know what to do. I am creating a application where the user can upload zip file. I want to store this file in my database. On Google I was not able to find any real solution for this. In my database I created a field of varchar(max) to store this. I am using C# and SQL server 2008 R2. Are there any solutions or guides that you can provide to me. >
You can not same file as nvarchar in database. Change the column datatype to varbinary.
From the code you need to convert your file to byte[] as like below.
byte[] bytes = System.IO.File.ReadAllBytes(filename);
Now save it into database as usual way.

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

Importing CSV to SQL Server CE SDF

I am trying to import a csv file into an SQL sever CE sdf database in a c# mvc3 web app. I understand that I need to read the csv file first and then transfer the file line by line into the sql database. I am very new to programming and need some guidence on how to do this.
I have heard Sql Compact Bulk Copy is the way to go, but I do not understand how to get the CSV file so it can be used by this. If anyone can shed some light on this I would be very greatful.
I hope this makes sense.
To create a new sdf File this is a litle example:
// if exists delete the file
System.IO.File.Delete("Test.sdf");
string cnn = "Data Source = 'TestDB.sdf'; LCID=1033; Password = <ThePassword>;";
SqlCeEngine ceEngine = new SqlCeEngine(cnn);
ceenngine.CreateDatabase();`
If you need to know more details you can read this MSDN(Create a Database by Using the Engine Object) http://msdn.microsoft.com/en-us/library/ms171966.aspx

how to do attachments for each sql record in c#

I am developing a desktop application in c# & sql 2005. With candidate data entry form I want to provide option to attach required documents(in pdf format) wid data. Kindly let me know the best method. Thank you in advance.
Simply create a table that will contain the filename and server path for the file to be attached, then create a method to copy the attached file to the server location and store the relivant information (name and path) in the table. Use other methods to retrive the file from the server location when requested. Simple.
I personaly prefer to store the documents as BLOBs since server file structures and paths can change over time.
Well then unfortunately you have to either manage the file storage yourself using the servers file system, or you could store it in the db itself (IT WILL GET BLOATED!!!)
See sql server 2005 file storage
How To: Encrypt and Manage Documents with SQL Server 2005
OK, then for file management
see this example
File Manager Component

Categories

Resources