How do I send multiple jpg files, as byte arrays, to my mySQL database with C#?
I have read and understand how to convert image files to byte arrays, but I can only figure out how to use that method for a single image to mySQL as a blob. My application requires the user to upload at least 2 image files and allows up to 10, while sending information from multiple textBox. I tried creating an array of byte arrays, but that didn't work. When I'd reference that array at a specific index during the INSERT string for mySQL, it seemed to store only one byte array and reference that byte array's index rather than the entire byte array that is supposed to be stored in that index. Below is some of the code showing my attempts:
///uploading the image and converting it to a byte array private void uploadButtonClick(object sender, RoutedEventArgs e) { chosenFileTextBox.Text = "No file chosen"; try { FileStream fs = new FileStream(imageFileNameArray[i], FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); imageFileArray = br.ReadBytes((int)fs.Length); ///Array of byte arrays imageArray[i] = imageFileArray; uploadedFilesTextBox.Text += imageFileSafeNameArray[i].ToString() + "\n";
The attempt at creating the insert string is below. The string worked when I sent only one byte array, so I changed the code to attempt 2 byte array's below, with no luck:
//open connection if (this.OpenConnection() == true) { //create command and assign the query and connection from the constructor try { MySqlCommand cmd = new MySqlCommand(query, connection); cmd.Parameters.Add("?_Date", MySqlDbType.VarChar).Value = dateString; cmd.Parameters.Add("?Sex", MySqlDbType.Text).Value = sexString; cmd.Parameters.Add("?FirstName", MySqlDbType.Text).Value = firstNameString; cmd.Parameters.Add("?LastName", MySqlDbType.Text).Value = lastNameString; cmd.Parameters.Add("?StageName", MySqlDbType.VarChar).Value = stageNameString; cmd.Parameters.Add("?Age", MySqlDbType.Text).Value = ageString; cmd.Parameters.Add("?Height", MySqlDbType.VarChar).Value = heightString; cmd.Parameters.Add("?weight", MySqlDbType.Text).Value = weightString; cmd.Parameters.Add("?Chest", MySqlDbType.Text).Value = chestString; cmd.Parameters.Add("?Waist", MySqlDbType.Text).Value = waistString; cmd.Parameters.Add("?Hips", MySqlDbType.Text).Value = hipsString; cmd.Parameters.Add("?Dress", MySqlDbType.Text).Value = dressString; cmd.Parameters.Add("?Shirt", MySqlDbType.Text).Value = shirtString; cmd.Parameters.Add("?Pants", MySqlDbType.VarChar).Value = pantsString; cmd.Parameters.Add("?Shoe", MySqlDbType.VarChar).Value = shoeString; cmd.Parameters.Add("?Email", MySqlDbType.VarChar).Value = emailString; cmd.Parameters.Add("?Phone", MySqlDbType.VarChar).Value = phoneString; cmd.Parameters.Add("?City", MySqlDbType.Text).Value = cityString; cmd.Parameters.Add("?_State", MySqlDbType.Text).Value = stateString; cmd.Parameters.Add("?Experience", MySqlDbType.VarChar).Value = experienceString; ///"?Image1" represents Image1 column. There are 10 columns, but for /// this example there are only 2. cmd.Parameters.Add("?Image1", MySqlDbType.Blob).Value = imageArray.GetValue(0); cmd.Parameters.Add("?Image2", MySqlDbType.Blob).Value = imageArray.GetValue(1); //Execute command cmd.ExecuteNonQuery(); //close connection this.CloseConnection(); MessageBox.Show("Connection Closed"); }