I want to save a pdf and mp3 file(s) to a SQL Server database and be able to retrieve from it.
I'm still starting out with LINQ and don't master it yet.
You need to convert these into byte arrays (System.Data.Linq.Binary). One line to load
var myMp3 = new Binary(File.ReadAllBytes(mp3Filename));
If you create your database schema (VarBinary in the database) and drag the table over from Server Explorer into the DBML designer, it'll do everything for you.
To start off with your going to have a binary field in your database to save the file to.
Are you using LinqToSql, EntityToSql, or? Need some more information...
But once you get an object with a []byte to save the file to then it just a matter of making the appropriate Save() call... but with having some more information it hard to say.
Did you google tutorials?
Here is one that I found: Uploading Binary files or Images using LINQ to SQL
Has example code and sql to generate dummy tables...
Related
From the DB schema that we have I think we are saving images in the SQL Server, I found a column called ImageContents(text null) in our Images table.
Now I have a folder that has bunch of PDF files and I want to save them in that table.
How do we do that? Why is it s text type column?
I Googled the topic but mostly they were saying save the image somewhere else and save a link to its address in the SQL Server but that is not how we do it so I couldn't find an answer by myself.
Since you're stuck with a text field, and you have binary data, you will need to encode your binary data into something that will go in a text column, and you will also have to decode it when you get it out again.
Base 64 encoding is one way to do this. (Note that the encoded form will take somewhat more space.)
Convert.ToBase64String(bytes) and Convert.FromBase64String(string) should get you back and forth, as in this example. (For the byte array step, use File.ReadAllBytes or see Vinikov's link.)
Again, it would probably be better to store a path and filename in the database, and keep the PDF files in the file system, but I understand you don't have the option to do that in this case.
P.S. SQL Server now has a FileStream attribute on varbinary(max) fields, that will store large files like this in the file system (though you have to enable it and configure it). Again, I understand that you don't have the option to use varbinary(max) in this case.
If you are already storing images in your database, look again and probably that you will find a column of type Image or varbinary(max). This is what you need to store your PDF files as they are binary files and not text files. Possibly that you can use the same type of code as what you are already using for storing your images; if such code is available. Otherwise, you shouldn't have any problem to find on the internet how to store an image file into a column of type Image or Varbinary(max). Storing a PDF file is exactly the same as storing an image file; except maybe for the extraction of metadata (such as decoding the width and the length of an image) and the checking of the suffix (.GIF, .PNG, etc.).
Do not use the column of type Text (or of varchar(max) or nvarchar(max)) because there would be a real possibility of corrupting your PDF files.
With the older versions of SQL Server such as 6.5 or 7.0; there was a real performance issue with storing files in a database but these problems have since been corrected.
I have an application that needs to upload .mdf and .ldf files and convert them into an excel file in the website and then allow them to be downloaded on the client computer. I have no idea how to start and what to do?
Can someone give me some ideas about how to convert a database in .mdf and .ldf files into excel file
Thanks in advance.
I think you can't simple Convert the file.
You should go through these steps:
Create a connection to your database
Select all Rows und Columns you need from you tables in the database
This should helpe you: ADO.Net
Create a new Excel-Sheet (Take a look at Open XML)
This should heklp you: Create Excel-Sheet
Fill you Excel-Sheet with the selected datas from your Database
Save the Excel Sheet
Now you are ready!
Have a read of this document which describes - How to import data from Microsoft SQL Server into Microsoft Excel
You're not going to be able to do this, for the simple reason that even after you upload the files you're not going to be able to attach them to the SQL server instance to query them (and then follow the process suggested by #BendEg).
Edit:
Seems I wasn't quite correct if you look at this link you can attach to a uploaded database by specifying the AttachDbFilename in the connection string.
Its easy just do the following
Right click your database-->GO to tasks-->Go to import data
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.
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
I would like to store images in database by converting them in to binary objects or i will store them in to a temp folder and will save the path in my database. But i am unable to do the programming so can any give me a sample code to save images in to sql database using Asp.net.
Here we go with some links:
http://www.beansoftware.com/ASP.NET-Tutorials/Binary-Files-To-Database.aspx
http://www.beansoftware.com/asp.net-tutorials/images-database.aspx
http://www.beansoftware.com/asp.net-tutorials/images-database.aspx
That should get you started, code wise.
better to store image in a folder and save the path in database, which would be much faster than saving in database.
Note dont use datatype image, which is going to change form database itself
: read this
Store the images path on DB instead of storing as Binary, because it hurts performance. In SQL server, BLOB datatypes are stored in seperate pages called LOB_data page and on data pages pointers will be linked with LOB_data page. You cannot create an index over BLOB data type also. Hence, it adversely affects the performance. Ideal solution would be to store the images in a shared drive and storing the image link in database.