Hey there =)

Im struggling to save a file to my MySQL database.

I managed to save a image to the database with this code

SqlConnection con = new SqlConnection(Properties.Settings.Default.NorthWestConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
            SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
            DataSet ds = new DataSet("MyImages");

            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            FileStream fs = new FileStream(@"C:\Users\Ruan\Downloads\Gone Fishing.BMP", FileMode.OpenOrCreate, FileAccess.Read);

            byte[] MyData = new byte[fs.Length];
            fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));

            fs.Close();

            da.Fill(ds, "MyImages");

            DataRow myRow;
            myRow = ds.Tables["MyImages"].NewRow();

            myRow["Description"] = "This would be description text";
            myRow["imgField"] = MyData;
            ds.Tables["MyImages"].Rows.Add(myRow);
            da.Update(ds, "MyImages");

            txtboxPassword.Text = MyData.ToString();
            con.Close();

But the thing is,this adds a new line. I simply want to add an image/doc to an existing row. (Preferably a document)

i usually use this code to update fields in my db.

SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Properties.Settings.Default.Studentsdb1ConnectionString;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            String SQL = String.Format("UPDATE Students SET First_Name = '{0}' , Last_Name = '{1}', Birth_Date = '{2}'  WHERE Student_Nr = '{3}'", txtName.Text, txtSurname.Text, Convert.ToDateTime(dateTimePicker1.Text).ToString("d"), SelectedStudentNr);


            cmd.CommandText = SQL;

            cmd.Connection = conn;

            object result = null;
            ConnectionState previousConnectionState = conn.State;
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                result = cmd.ExecuteScalar();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
          
            
            finally
            {
                if (previousConnectionState == ConnectionState.Closed)
                {
                    conn.Close();
                }

So now i cant seem to store a file? I am missing something somewhere.
Could you please help me out?
Thankss =)
Ruan

Recommended Answers

All 5 Replies

Well, gues daniweb Sucks, im going over to Stackoverflow.com
Does not seem that this form has people supporting it.
Chowwww

oh and i found out how to do it btw =)

Could also be it's the middle of the day and people are working.

Care to share your solution to the problem for others?

Could also be it's the middle of the day and people are working.

Care to share your solution to the problem for others?

Hey there Zachattack, Thank you for your responce.

It might be the reason.. Buttt, im sure that not all Daniwebs users are "in the middle of the day and working" all at the same time for 2 straight days.

I would love to post my answer, but im not going to post it on this form.
To find the answer to my question i posted it on Stackoverflow.com (and it was answered within 3 hours... Guess The people from Stackoverflow dont have middle of the days, or work.)
http://stackoverflow.com/questions/5972215/mysql-save-file-to-database

I do not have anything against the Posters from this site, its just that if one site is more popular and more helpful, i would rather support that one.
Thanx
Ruan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.