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.
Related
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 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.
How can i use copy command for bulk insertion of data into postgresql table from csv file present in remote machine using C#?
I've a front end in C# using which i need to load data into postgresql table from csv file. The database is in remote server and the csv file is in my machine.
Assuming the CSV file is on the machine running the C# program, you need to use COPY ... FROM STDIN. This is difficult to use directly from client drivers, but most of them support their own interfaces on top.
I'm guessing you are using nPgSQL since you didn't bother to mention what client you're using. If so, you can use NpgsqlCopyIn.
Not possible at all, unless you mount the remote machine on the postgres server. To copy postgres needs to be able to access the file locally.
You could try to scp the file from A to B, or parse the file yourself and do bulk inserts into postgres, ie:
create table file (structure of the file);
Read 100 lines
insert into tabe file
values (line 1)
,(line 2)
...
,line (100)
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
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