I have a C# application that saves pictures in a SQL Server database. SQL Server keeps pictures in a column of type varbinary(max). And the app converts byte[] to show the picture on my screen.
What I want to do is to check the picture whether it was added before. First of all entire database kept by dataset. Then, I tried to compare pictures via checking equality of byte arrays of images. But It does not work. Because (depends on my understanding) the byte array of the picture (which I was intended to add via openfiledialog), does not match the byte array of the picture (which comes from dataset) even they're all same picture (same name, same location, same size etc..)
This brings out more important question on my .Net, C#, SQL Server and ado.net learning.. In such situations, which way is more suitable? To check the data existence on business layer, or on data layer? (sorry my bad english)
You may want to consider storing an md5 hash of the image, and use that as the comparison. See this answer: https://stackoverflow.com/a/8802486/214919
Many times what we do is save a few additional fields in the table. Like "original file name" which is the name of the uploaded file. The file size, the user, and as mentioned above an md5 hash of the image. This way we can save a lot of work by doing simple compares early in the business logic, and only allow for a full heavy-workload compare at the level you are trying to do.
Related
I have setup filestream on my mssql server, and it works pretty well thus far. Currently, I have one entity in my database, that I have added manually.
When I added my file, it was automatically converted to a byte[], which Similarly appears as a byte[] when I get it through my .NET Core application (surprise).
Optimally, I would like to know, how I can decode this byte array into the original file? I have read several places, that I need to provide the original extension of the file in order to do that.
However, I have not added such a column in my database - I could easily add such a column, but it seems odd to me, if it isn't possible to pass it back to its original format without providing additional parameters.
Therefore, is there a way, in which I can convert the byte array back to its original file so that the user easily can download it, without having to do some sort of comprehensive conversion?
I would happily like to know, if one of you can point me in a direction here.
A filestream column contains the file's contents, not its external metadata, like the original filename, extension, directory location and access control list. If you write the byte[] to disk with, eg, File.WriteAllBytes(string,byte[]) it will be a usable file.
I will create MySQL database for online store. This store has around 3000 products. Each product has 5 photos.
I created tables for products table (product_id, product_name) and images table (image_id, Image_details, Product_id)
What is best way to insert photo?
*i am using C# (desktop app) as GUI
Although, with BLOB objects you can achieve this, it not the preferred way. You have to store the location of the images (similarly for other large files), when the query result turned, use the location to access the images.
Keep in mind, your images will be increased over time, so, in practice, we apply a directory hierarchy, usually two will be enough. One can use the first two letter of the name of the image, however, this will be unbalanced directory structure. The better idea is using hash based randomization to distribute the files evenly over the sub-directories. An example (in Java) can be found here.
Dont save your images in the database, it's not the best way. save the paths to the images in your table related to the products.
Use cloud Blob storages or FileSystem instead of saving in your relational database. It is not recommended storing a lot of images >1mb in your database due performance, backup and maintenability.
However if you have a small app and are really sure about going this way, you could convert your image to a byte array and save in a MySQL BLOB field. Check the link below for a sample:
C# saving images to MySql database as blob
Check:
Should I store my images in the database or folders?
How can I save an Image to the File System?
https://learn.microsoft.com/pt-br/azure/storage/blobs/storage-quickstart-blobs-dotnet?tabs=windows
https://www.c-sharpcorner.com/article/azure-storage-crud-operations-in-mvc-using-c-sharp-part-two/
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 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.
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.