Page 1 of 1

Can anyone save image to MySQL from Pocket PC?

Posted: Thu 07 Apr 2005 17:23
by john_elden2002
I want to save images (bmp, jpg, tif...) to MySql database using my Pocket PC. I am using MySQLDirect.net 2.70.40 trial version. Below is my code. Can anyone help me?

Code: Select all

		OpenFileDialog fdlg = new OpenFileDialog();
		fdlg.InitialDirectory = @"" ;
		fdlg.Filter = "Jpeg files (*.jpg)|*.jpg|MS bitmap Files (*.bmp)|*.bmp" ;
		fdlg.FilterIndex = 2 ;

		if(fdlg.ShowDialog() == DialogResult.OK)
		{
			FileStream fs = new FileStream(fdlg.FileName, FileMode.Open, FileAccess.Read);
			byte[] buffer = new byte[fs.Length];   
			fs.Read(buffer, 0, (int)fs.Length);
		    fs.Close();
			// Method 1
			dataSet = new System.Data.DataSet("Image");
			string strSql = "select * from file";
			MessageBox.Show(connection.ConnectionString);
			mySqlConnection = new MySqlConnection(connection.ConnectionString);
			mySqlConnection.Open();
			mySqlDataAdapter = new MySqlDataAdapter(strSql, mySqlConnection);
			mySqlCommandBuilder = new MySqlCommandBuilder(mySqlDataAdapter);
			mySqlDataAdapter.Fill(dataSet,"Table");	
	
			dataRow = dataSet.Tables["Table"].NewRow();
			dataRow["id"] = 1;
			dataRow["extension"] = fdlg.FileName;
			dataRow["data"] = buffer;
			dataSet.Tables["Table"].Rows.Add(dataRow);	
			// trying to update the table to add the image
try 
{
			mySqlDataAdapter.Update(dataSet,"Table");		
}
			catch (System.NullReferenceException msg) 
			{
				MessageBox.Show(msg.Message, "SQL error");
			}
			finally
			{
				mySqlConnection.Close();
			}			
			// End method 1

			// Method 2
			/*MySqlConnection mySqlConnection = new MySqlConnection(connection.ConnectionString);
			MySqlBlob myBlob = new MySqlBlob(buffer);
			MySqlCommand myCommand = new MySqlCommand("Insert into file (id, extension, data) values(1, 'something', :MyData);", mySqlConnection);
			//MySqlCommand myCommand = new MySqlCommand("Insert into file set id=1, extension='something';", mySqlConnection);
			myCommand.Parameters.Add("MyData", myBlob);
			mySqlConnection.Open();
			try
			{	
				myCommand.ExecuteNonQuery();
			}
			catch (System.NullReferenceException msg) 
			{
				MessageBox.Show(msg.Message, "SQL error");
			}
			finally
			{
				mySqlConnection.Close();
			}*/
			// End method 2
		}
Method 1 works on PC, but throw NullReferenceException when running it from Pocket PC. Method 2 works without save the image to database from my Pocket PC. I copy method 2 from someone on the forum, but I could not get it working.

Thanks

John

Posted: Mon 11 Apr 2005 09:35
by Serious
We have fixed this problem (NullReferenceException) for the MySQLDirect .NET Mobile.
Look forward for the next build.

New build will be released in a few days.

Posted: Tue 19 Apr 2005 23:06
by john_elden2002
When will the new build release?

Posted: Wed 20 Apr 2005 07:08
by Serious
The new release will be issued in few hours

Posted: Thu 21 Apr 2005 21:52
by john_elden2002
Thanks. The new release works to save image to MySQL.

How do I read the image from MySQL and save it to a file? Thanks.

Posted: Fri 22 Apr 2005 11:05
by Serious
There is a "Pictures" demo project in MySQLDirect .NET Mobile installation folder.
It demonstrates using MySQLDirect .NET Mobile for working with graphics. The sample demonstrates retrieving binary data from MySQL database and displaying it at the monitor, loading and saving pictures into the file and database.