Adding an image file to a database with C# and ASP.NET - c#

How can I add an image file (for example, JPEG, PNG, GIF ...) to a database with C# and ASP.NET?
I'm just working on the project. And now I need your help.

The best way is to simply store the path of the image in the database. Answered fully here and here

If you want the actual image in the DB, then you will have to perform what is referred to as "Blobbing". You will need to convert the image into a Byte Array(Byte[]) and then store the array in the database. The field in the database is going to need to be appropriate for storing a Blob.
Then when you load the image from the DB (to display it), you will have to convert it back into an image. It would probably be useful to store the image type along with the image data so that you know what format to convert that image data into.

Try this
http://www.aspsnippets.com/Articles/Display-Images-in-GridView-Control-using-the-path-stored-in-SQL-Server-database.aspx

Related

How to convert .shp file into an array?

I am using EasyGis.net to load a shape file in my desktop application.
I want to save this file in database so that i can use this file from the server side of my software.
I am able to save normal .png files in database by converting image object into a byte array.
Now i need to save the shape file so that i can use this from my server.
You can use this function:
File.ReadAllBytes(fileName);
Ref: https://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v=vs.110).aspx

Saving PDFs in SQL Server

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.

The best way to store picture with SQLite and nHibernate

The application is written in C#. It uses nHibernate and SQLite and runs on Windows. I should store pictures and bind them to a user.
So far, I don't really know what is the best way to store the pictures. If I decide to store the path of the picture and save the image in a directory, I'll have problems as soon as the directory or the file is renamed or moved. In the other hand, storing image as BLOB in SQLite is slow as I have to "cast" the image into a bytes array before storing and when I retrieve, I have to set the bytes array into an image.
What is the recommended way for this?
I wrote an image user type for this purpose. With it, you just pass the Image object, not its bytes: http://weblogs.asp.net/ricardoperes/archive/2009/09/17/nhibernate-image-user-type.aspx.

comparing image from ui to saved image in SQL server database Asp.net, c#

I had developed an application in Asp.Net which saves image in SQL Server database it fines, but i want a scenario like this When i am trying to save same image again then it should gives me message that the image is duplicate. How to do this? How we can compare a image from UI & SQL database in Asp.net?
please help me friends
Thanks
Well, if the image is exactly the same, then you could make a hash of the image object which is saved in a separate column, and check if there are any columns that matches any entries in that column whenever you try to save an image.
If you need to check if the image is similar or just looks the same, then you need more advanced image recognition software..

Can anyone tell what is the best way to store images in SQL

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.

Categories

Resources