I need to upload an pdf to a column in SQL Server existing table.
SELECT TOP (1000) [Id]
,[Name]
,[EntryDate]
,[Attachment]
,[IsActive]
,[Remarks]
FROM [db].[dbo].[Mst]
i want upload pdf files directly in column Attachment.
How can i do?
Amit MohantyPosted May 2, 2023, 8:21 AM
Try this
Vishal YelvePosted May 2, 2023, 8:11 AM
Hi Garima Bansal,
Yes as per Rajkiran sugestion, we cannot directly save file in table we can store file on some location (file system) and we have save that file location (file path) in db. Thats why i share you below link
https://www.aspsnippets.com/Articles/Save-and-Retrieve-Files-from-SQL-Server-Database-using-ASPNet.aspx
Rajkiran SwainPosted May 2, 2023, 7:49 AM
It is not recommended to directly upload binary files such as PDFs to a database column. Instead, it is best practice to store the file on a file system or in cloud storage and store the file path or URL in the database column. This approach can help reduce the size of the database and improve performance.
Here are the general steps to follow:
Here's some sample code in C# using ADO.NET to insert the PDF file path into the database table:
In this example, the
@Attachmentparameter contains the file path or URL to the PDF file. You will need to replace the connection string and parameter values with your own values.Garima BansalPosted May 2, 2023, 7:44 AM
Rajkiran Swain
INSERT INTO [db].[dbo].[Mst] ([Name], [EntryDate], [Attachment], [IsActive], [Remarks])
VALUES ('Example', GETDATE(), (SELECT BulkColumn FROM OPENROWSET(BULK N'C:\Users\dell\Desktop\Aadharcard.pdf', SINGLE_BLOB) AS Document), 1, 'Example remarks')
I tryed your query but getting this error
Cannot bulk load. The file "C:\Users\dell\Desktop\Aadharcard.pdf" does not exist or you don't have file access rights
Rajkiran SwainPosted May 2, 2023, 7:36 AM
You can use SQL Server's `INSERT INTO` statement to add a PDF file to a table's column. Here's an example:
This query assumes that your PDF file is located at `C:\example.pdf`. It uses the `OPENROWSET` function to read the binary data of the PDF file into a `varbinary(MAX)` column in the table. You can replace the values in the `VALUES` clause with the appropriate data for your use case.
Garima BansalPosted May 2, 2023, 7:25 AM
is it possible to upload PDFs in to SQL Server not using code
With any query
Vishal YelvePosted May 2, 2023, 7:12 AM
Hi Garima Bansal,
Do refer below link, It will help you.
https://www.aspsnippets.com/Articles/Save-and-Retrieve-Files-from-SQL-Server-Database-using-ASPNet.aspx