Click here to Skip to main content
16,020,249 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to insert image into the database, the Button that opens the Filepicker converts bitmapImage as I can make the image save to the database through the save button. Also read the database for image


in the part of showing the data a DataGrid (windows Community toolkit) is used


What I have tried:

Insert code for Data Base:

C#
private async void BTInsert_Click(object sender, RoutedEventArgs e)
        {

            try
            {

                const string SqlString = "insert into PetTable(Pitcure,PetType,DuoDate,PetName,PetRace,PetColor,PetChip,PetGender,PetSterile) values (@picture,@pettype,@duoDate,@petName,@petRace,@petColor,@petChip,@petGender,@petSterile)";


                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();

                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        using (SqlCommand cmd = conn.CreateCommand())
                        {


                            cmd.CommandText = SqlString;
                            cmd.Parameters.Clear();


                            cmd.Parameters.Add("@picture", SqlDbType.Image).Value = Photo;


read for database

C#
<pre>public static async Task<ObservableCollection<PetTable>> GetPets()
        {
            const string query = "SELECT PetType, DuoDate, PetName, PetRace, PetColor, PetChip, PetGender, PetSterile  FROM PetTable";

            var pets = new ObservableCollection<PetTable>();
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    await conn.OpenAsync();
                    using (SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    var pet = new PetTable
                                    {
                                        //Picture = reader.GetByte
                                        PetType = reader.GetString(0),
                                        DuoDate = reader.GetDateTime(1),
                                        PetName = reader.GetString(2),
                                        PetRace = reader.GetString(3),
                                        PetColor = reader.GetString(4),
                                        PetChip = reader.GetString(5),
                                        PetGender = reader.GetString(6),
                                        PetSterile = reader.GetString(7)


                                    };


                                    pets.Add(pet);

                                }
                            }
                        }
                    }

                }

                return pets;
            }

            catch (Exception ex)
            {
                var Msg = new MessageDialog("Exeception:" + ex.Message);
                Msg.Commands.Add(new UICommand("Close"));
            }
            return null;
        }

open filePicker

C#
private async void BTPhoto_Click(object sender, RoutedEventArgs e)
        {
            var OpenPicker = new FileOpenPicker();
            OpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            OpenPicker.FileTypeFilter.Clear();
            OpenPicker.FileTypeFilter.Add(".jpg");
            OpenPicker.FileTypeFilter.Add(".jpeg");
            OpenPicker.FileTypeFilter.Add(".png");


            var file = await OpenPicker.PickSingleFileAsync();
            if (file != null)
            {
                using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))

                {
                    BitmapImage bitmapImage = new BitmapImage();

                    await bitmapImage.SetSourceAsync(fileStream);

                    Photo.Source = bitmapImage;
                }


            }

        }
Posted
Updated 7-Dec-21 10:52am

1 solution

Photo is not "raw data" - it's a class which contains image data. So when you try to pass the class to SQL, what gets passed is not the image data - it's class information. SQL doesn't know what to do with that as it basically expects raw bytes.

Instead, pass the actual image source data and rebuild the C# class when you retrieve it.
This should help: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]
 
Share this answer
 
Comments
javier ramos lopez 10-Dec-21 16:06pm    
Photo is control Image
OriginalGriff 10-Dec-21 17:29pm    
Yes. It's a Control, which contains an image. That isn't the same thing as the raw data for an image, just like a page from an address book contains references to houses, rather than the houses themselves ...
javier ramos lopez 12-Dec-21 12:02pm    
I have a problem with save from the code to insert into the database that comes in the example
OriginalGriff 12-Dec-21 12:22pm    
What problem?
What example?
javier ramos lopez 12-Dec-21 16:17pm    
the example of inserting into the database the problem is with Image.Save(). in the image = myImage = image part I don't need the path

Image myImage = Image.FromFile(@"D:\Temp\MyPic.jpg");
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
data = ms.ToArray();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900