0

I Try to save a File in DB.

I get the File from OpenFileDialog and save it in a Stream.

This stream i Read to a String and try to save it in a MySQL DB.

It Woks with smole .txt Files.

But not with .jpg. (to big string?)

I get ERROR: "MySql.Data.MySqlClient.MySqlException" in MySql.Data.dll

public void UplodeFile(int user, Stream filestream)
        {
            StreamReader streamReader = new StreamReader(filestream);
            string filestring = streamReader.ReadToEnd();

            string query = "INSERT INTO datein Values " +
                "(NULL,'" + user + " ','" + filestring +"');";
            
            MySqlCommand commandDatabase = new MySqlCommand(query, databaseConaction);
            commandDatabase.CommandTimeout = 60;
            try
            {
                databaseConaction.Open();

                MySqlDataReader reader = commandDatabase.ExecuteReader();
                databaseConaction.Close();
            }
            catch (Exception e)
            {
                Console.Write("error: " + e.Message);
            }
        }

I Use in db datatype LONGBLOB.

I have try binary it dosen't work

Werter
  • 1
  • 1
    The *type* of the exception can be useful to know, but the *message* of the exception is really what you need to find out... – David Nov 14 '22 at 17:54
  • 3
    The first problem is that JPEG files *aren't text*. You shouldn't be trying to read them as text. You should read them as binary data, e.g. with `File.ReadAllBytes`. Also, read up on SQL Injection and parameterized queries. – Jon Skeet Nov 14 '22 at 17:54

0 Answers0