Loading text stream from c# to sql server without saving physical file? - c#

I have a text stream that I can save as a txt file and then call sql server stored proc to bulkinsert that txt file.
But I don't want to deal with file system access and all that stuff. and sqlBulkCopy can't do it I beleive. What's the solution then?

If the incoming stream represents rows for a table, then you can write a custom IDataReader implementation that reads from the stream and presents each row in turn (non-buffered). You can then feed this to SqlBulkCopy.
Example: https://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/b1d70b504cdee2ad?hl=en&pli=1

Related

How to save binary data back as a file from SQL table

I am using Microsoft SQL server database file (file with .mdf extension) as the database.
I managed to save files into the SQL table as varbinary(max). Now my question is : How do I get back the files?
I am thinking I may still need to use SELECT statement to get the file from the SQL table. However how do I save the file to my hard disk from its current binary format? What are the procedures to do that?

Show PDF file from SQL Database

I already uploaded pdf files into my sql database as binary data file. I want to retrieve this file and open it with Adobe Reader on my form. But I couldn't solve it in C# code.
I want to retrieve the pdf file which id number = textbox1 and I want to load that file to axAcroPDF1 Adobe viewer.
I want to write this code to form load action.
textBox1.text= clipboard.gettext();
then retrieve code.
You first need to retrieve the data from the database in the same way as you would any other data. That will give you a Byte array. You can then call File.WriteAllBytes to write that data to a file. You can use the Path.GetTempFileName method to create a temp file to write the data to if appropriate. You can then use that path to open the file in your PDF reader as you normally would.

LOAD DATA LOCAL INFILE in C# from memory

Is there a way to use the LOAD DATA LOCAL INFILE command to load data from memory in c#?
The closest I can come up with is executing a command like:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
I'm trying to avoid needing to write a file to the disk (fun with permission issues) on the client's system.
No you cant. It must read fom a file on disk. Txt or csv are most common.
http://dev.mysql.com/doc/refman/5.1/en/load-data.html

How to save a csv file into a database?

I am making a window application in asp.net using C#. I want to browse and import .csv file and save it in the database. Importing part I have done. Now, how am i supposed to save it in the database ? I am new to the language so please help
You can use ADO.NET, you will need to look up Data Adapters and the like.
If your Importing the Spreadsheet into a DataTable or DataSet you can use a Data Adapter to populate a table in the Database using an INSERT command.
Look up http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.aspx
Hope This Helps.
Once you have a file upload mechanism in place, (for example, using the FileUpload control), just iterate through the lines in the CSV file on the server, parse them into fields using an existing CSV reader (don't try to code your own unless your data is completely trivial, you won't handle all of the edge cases), and just execute a bunch of SQL INSERT statements against your database using your calling convention of choice.

C# app to select data from MySQL and insert to SQL-server (blob files)

I want to run a query on a MySQL database and insert the results in a SQL-Server 2008 R2 database.
In my table (MySQL database) there are multiple columns and one of them contains a file path. I want to use that path to insert the actual file as BLOB in my SQL-server.
So all columns from MySQL need to be inserted in SQL-server and the actual file as BLOB.
I can connect and query the MySQL database and also connect my SQL-Server.
But how can i insert the results. (some files are very large !!)
I found someting about OPENROWSET, but i could not find a good example inserting the metadata and the file.
I want to write a C# app for this. I appreciate any help.
SQL-Server 2008 R2 Support file stream. (http://technet.microsoft.com/en-us/library/bb933993.aspx)
BLOBs can be standard varbinary(max) data that stores the data in tables
FILESTREAM varbinary(max) objects that store the data in the file system
If Objects that are being stored are, on average, larger than 1 MB you shoul go with filestream.
As #Pongsathon.keng mentioned in his response FileStream is an option. You need to enable your DB to support FileStream and the user that is writing cannot be a sql login (Keep that in mind). You can use a varbinary(max) also mentioned (Image will be deprecated).
We decided to go with FileStream so we could also utilize it in conjunction with FUll-Text Indexing. You could also go with the typical "Store File Path" approach and store the actual files / blogs in a file system. FileStream gives you the added benefit of backing up files with the DB and apply permissions / transactions, etc.
Really depends on what you are doing.
Some good articles on FileStream Here and Here

Categories

Resources