Images are one of the primitive objects that one can think
of to store in the database. Apart from these, we can also store customized
objects in the database. But for this purpose, the class must be serialized.
Let us consider an example of an AppSettings class. Here we will save the
setting of an application like the window size, position, etc. to a table named
SETTINGS.
Listing 3
[Serializable]
public class AppSettings
{
public int screenX;
public int screenY;
public int screenW;
public int screenH;
public DateTime lastLogin;
}
The above class can now be used to store the last used
settings in the database and on the next login can be retrieved and applied to
the application. The example here is a simple class, but this can be customized
according to the requirements.
The serialization of the AppSettings object is same as the
Image example given above. Here we will use the following two functions.
SerializeNSaveToDB()
This function first creates an AppSettings object and
initializes all the required variables and serializes it into a MemoryStream.
It then calls the Call_Upload_Object() function to save the serialized object
to the database.
DeserializeNSaveToFile()
This function fetches the serialized AppSettings object from
the database with the help of MySqlClient Classes. The object is converted into
a MemoryStream object which is then de-serialized with the help of a
BinaryFormatter. This is in turn converted into an AppSettings object and the
settings can now be easily applied to our application.
Listing 4: Working with Custom Objects
private void SerializeNSaveToDB()
{
MemoryStream str = new MemoryStream();
try
{
string column = "?OBJECT";
AppSettings oSettings = new AppSettings();
oSettings.screenX = this.Left;
oSettings.screenY = this.Top;
oSettings.screenW = this.Width;
oSettings.screenH = this.Height;
oSettings.lastLogin = DateTime.Now;
BinaryFormatter oBFormatter = new BinaryFormatter();
oBFormatter.Serialize(str, oSettings);
byte[]oSerializedObj = str.ToArray();
string Query =
"INSERT INTO SETTINGS (NAME, SETTINGS) VALUES('Settings 1', ? OBJECT)
";Call_Upload_Object(Query, oSerializedObj, column);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
str.Close();
}
}
private void DeserializeNSaveToFile()
{
DataTable ds = new DataTable();
MySqlConnection oMySqlConn = new MySqlConnection(MySqlConnString);
try
{
string Query = "SELECT * FROM SETTINGS";
MySqlCommand cmd = new MySqlCommand(Query, oMySqlConn);
oMySqlConn.Open();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
MemoryStream oStr = new MemoryStream((byte[])ds.Rows[0]["SETTINGS"]);
BinaryFormatter oBFormatter = new BinaryFormatter();
AppSettings oSettings = (AppSettings)oBFormatter.Deserialize(oStr);
this.Left = oSettings.screenX;
this.Top = oSettings.screenY;
this.Width = oSettings.screenW;
this.Height = oSettings.screenH;
txtLastLogin.Text = oSettings.lastLogin.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
oMySqlConn.Close();
}
}