I have a console application using C# where I want to read the blob stored in SQL Server and write it to windows file system at a specified path.
How do I convert a blob to a image of pdf using C# and write it to a file system?
Read the blob from the database into a byte[] and write this buffer to a file.
Take a look here: http://www.akadia.com/services/dotnet_read_write_blob.html
Using a DataAdapter / DataSet would most likely prove even easier - if you can afford to have the entire BLOB content loaded into memory for the number of rows you're processing.
What kind of blob are we talking about here? Assuming you are using .NET, Check out SqlDataReader or Entity Framework. There are other methods as well but those 2 are pretty popular. Converting to PDF you will probably need a 3rd party tool, check out PDF Sharp. Finally, saving to filesystem is pretty straightforward, look at the .NET System.IO.File class
Related
Im using C# and Service based database and I need to import some data from Excel to my database ..How could I possibly do this?? Please help. Thanks a lot.
You can open the excel file with the Excel database driver and read it like any other data source, however this means you need the driver, which isn't installed by default.
Download
HowTo
However if the sheet only contains data and doesn't need any calculations, you can unzip the XLSX file, and find sheet1.xml (or whatever it's called in your file), open it in your app like any other XML file and import the data.
This is likely to be a much better long term solution, since MS has been trying to kill off the Access database driver for ages.
Also, it's been a while, but I don't believe MS recommends using the MSDE from within a service.
I would recommend you to use OfficeOpenXml.Core.ExcelPackage or EPPlus to read/write excel files. Bellow is some links to reference
https://www.nuget.org/packages/OfficeOpenXml.Core.ExcelPackage/
https://github.com/JanKallman/EPPlus
https://www.c-sharpcorner.com/article/import-and-export-data-using-epplus-core/
https://tedgustaf.com/blog/2012/create-excel-20072010-spreadsheets-with-c-and-epplus/
http://www.talkingdotnet.com/import-export-xlsx-asp-net-core/
https://toidicodedao.com/2015/11/24/series-c-hay-ho-epplus-thu-vien-excel-ba-dao-phan-1/
http://www.zachhunter.com/2015/11/xlsx-template-with-epplus-and-web-api-save-as-xlsx-or-pdf/
I want to store and retrieve pdf files into/from SQLITE database, I'm using C#. Any one with an Idea of how this is done?
You could read all the bytes of the PDF, store that and then do the same the other way. although i would think that saving the PDF on an internal storage and storing the path would be a better option.
I'm exporting data from SQL Server using BCP. The output file is written to disk, but needs to be re-encoded. Using C#, I re-encode the file to UTF8 and re-save it to disk. As is, it has to save the output, re-encode, and then re-save it. Seems inefficient.
I'm wondering if there's a way to eliminate saving the data twice. For example, direct the output to memory or intercept the BCP output file in memory before it's written to disk?
Any advice is greatly appreciated.
Load the data in your C# application and write it to disk using any format you like. BCP does not have inherent efficiency advantages compared to a custom app. You might use more CPU in ADO.NET + C# than the C++ based BCP uses. This is to be tested.
You should look into Windows named pipes. Similar to the Unix concept but not as easily created.
This blog post provides a tool that sets up a pipe and automatically compresses the "file" data that is written to it. We use this to compress bcp output without writing an uncompressed file to disk.
The code is provided so you "should" be able to modify it to convert the encoding instead of compressing the data.
Say you have a method or property on a third party sealed component that expects a file name. You do not have the source for the third party component. You want that component to do what it's supposed to do (read only) on a file you have, but your file is encrypted on disk and you only want the decrypted version in memory so it cannot be easily copied in its plain form.
Is it possible to create a wrapper or some other approach to trick the component to think it's reading from a file when it's actually reading from a MemoryStream? Or is it totally impossible? Can it be done outside .NET in native Windows code?
Thanks
You can't do that the way that you are proposing, no. My recommendation would be to use the Encrypting Filesystem functionality built into windows. That way the file is stored in encrypted form on disk, but is available via the normal IO methods to the application (provided that the account that is running the application has access to the file).
Can it read from "CON" as input (like many text utilities grep/findstr, more,...)? In this case you can try to redirect input/output stream and feed results thata way.
Is it possible to create a wrapper or some other approach to trick the
component to think it's reading from a file when it's actually reading
from a MemoryStream?
No, sorry. You will have to decrypt the file into a temporary file and then pass this temporary file to the plugin. Once it finishes its work delete the temporary file.
This short answer is if a component is expecting a filename e.g. a string you can not parse it a memory stream.
However if the file is encrypted with Encrypting File System (EFS) or something native to Windows it may be able to decrypt the file without knowing the file is encrypted.
These might help:
http://en.wikipedia.org/wiki/Encrypting_File_System
http://en.wikipedia.org/wiki/BitLocker_Drive_Encryption
You could have a look at Dokan. I haven't tried it, but it's a way of creating a virtual file system in .Net.
You can create an in-memory disk drive (either in code or by using third-party application) and put a file there. Another approach is to implement virtual file system and handle all file requests. Both approaches are possible for example using our Virtual Storage products.
Also, I don't know about .NET in particular, but in Windows you can hook API functions and substitute certain operations (including file operations). There even exist components for this, but, again, I don't know if they offer their functionality for .NET.
Consider i have an excel file >200000 rows in it. What is the fastest way that can be implemented to search a partcular column value in this file using c# asp.net. Any suggestion.
Assuming 1) you can cache the file contents fine (not too large, file doesn't change, etc) and 2) you don't already have a mechanism for reading the file, I would just read the file once (at application start, or lazy load on demand, or whatever) into memory - I have used and really like the FileHelpers libs from http://www.filehelpers.com/ - see their excel example # http://www.filehelpers.com/example_exceldatalink.html
as part of the 'read in the file', you'd likely also create some indexes for the later queries. If you only cared about the one column, you could just push it all into a HashSet, for instance, so you can do a Contains later quickly.
You cannot access an Excel file from ASP.NET at all if you are using the Excel Automation APIs. These were written for use in a desktop application, not in a server application like ASP.NET. They will not work, are not supported, and may very well violate your license agreement with Microsoft.
There are third-party libraries that can access an Excel file safely from ASP.NET. These do not use the Automation APIs.
You may want to consider using an "OLE DB for Jet 4.0" connection, which you can query via ADO.NET. OLE DB access to Excel is provided via the MDAC component, which comes standard on versions of Windows after 2000. ConnectionStrings.com has OLE DB connection strings for connecting to Excel, as well as information on using Jet in a 64-bit environment.
Use EPPLus and read the file into an DataTable.
Could take some time, that file is a little bit big...