How to insert an image from a MediaPicker into my database in C# (MAUI)
Hello! 🙂 I'm new to MAUI and I'm facing challenges trying to insert an image from a MediaPicker into my database.
What I do is obtain either my byte array using these lines:
or obtain the data in a base64 string value using this line:
The problem is, once I obtain these values that I need to insert into my database, and I execute the insert query, I'm getting this error:
Microsoft.Data.SqlClient.SqlException: 'A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - Success)'
This only happens if i'm including the image in the query, if I eliminate it, the other data can be inserted perfectly without showing me any error.
This is my query:
And this is my connection string:
Thank You!
12 Replies
General rule is to never store images in database, but rather a path to an image on the file system.
Well, in my case, what I'm doing is allowing a user to register information along with an image. My goal is for all devices to access this user-entered information and also view the image, which is why I was trying to save the image in my database.
If I save only the path, when retrieving the information, the path will only work for the device that uploaded the file, and others won't be able to view it.
I should also mention that if I generate the query within my database manager and execute it, the data is inserted without any issues. The error only occurs when I do it from the app.
That's why you save it either in a cdn or to your own filesystem, and then make it available for the user as an image itself
And not as a path
Unless you're using a cdn, then you can provide the url to the application
Typically, you store uploaded images on the same machine where your database is stored.
So, when users upload an image, you save it in your file system and then write the path to that image in your database.
Whenever someone wants to see the image, you use the stored path to serve the same image from your file system.
So, if the user takes a photo from their cellphone, then I store the image in the system and save the path in the database, upon retrieving this path, can any device view it even if this device didn't take the photo?
Where is your database located?
Is it on the server somewhere?
Or does every device have their own database?
The database is on a hosting server.
So that's where you should be uploading the images too.
Paths stored in database should point to images stored there.
Perfect! Thank you for your help
@MarcoART_ Just want to point out that if you're going to host on services like Azure, where you can't access the filesystem of the the database server (in 99% of the cases, but for now let's assume), you might want to store the image in a Blob, either public or private. If private, your application can then easily generate urls to serve that image without you hitting the blob since you know the exact path.
Backblaze B2 is pretty much "free" for small applications btw
Thank you!