Is there any possibility to attach PDF files or any type of file to a DB record in SQL Server 2008 R2 from a C# application made with VS 2012.
Is there any possibility to attach PDF files or any type of file to a DB record in SQL Server 2008 R2 from a C# application made with VS 2012.
If possible. You have three alternatives:
Store file directly in database as type
Blob
and store binary directly.Store file directly in database with type
FileStream
.Store the file on disk and store the file path in a field of your table in the database.
Each alternative has its pros and cons after several discussions about it. The most recommended I've seen for SQL Server (at least on images, but it applies to any type of file) is the second . In case you do not use SQL Server, the second option is ruled out and I would recommend the third because it facilitates the maintenance of the files, the database is not so heavy, there is a separation of issues, it facilitates the tests, etc.
Here more information about it: https://stackoverflow.com/q/3748/1065197
Yes it is possible, although it is not recommended
You can declare your column like
VARBINARY
in SqlAnd the code to upload the file would be more or less something like this:
Working with files within the database is not a very good practice, since inserts and requests are more expensive than simple text or some other numeric type. But if it is possible.
This topic is touched on in English here, take a look ;)
It doesn't really matter what version of the IDE you use, with C# you can do many things, and among them you can choose to serialize the file (PDF or any other) to a Base64 string and save it as a string:
Here
[ASP.NET] Save File to Database
I explain how you could insert a file into a field in the table
As you can see, a field of the type is defined in the table
varbinary
and to insert you must passINSERT
an array of bytes as a parameter.If your app is
winforms
very simple you just use the File.ReadAllBytes ()In the example of the link the code that is useful is in the persistence layer
generating a parameter in the INSERT that allows assigning the byte[] of the file
I would also recommend validations if it is not useful for you to define the field as
FileStream
, for the versionSql Server
you mention you can use it and it is more optimal when registering files in the db.An Introduction to SQL Server FileStream
How Do I: Use SQL File Stream