Object Persistent Utility… Store any object in DB

Hi Everybody,

Last night i got a requirement in my project that if user fills the form and clcik on the save button or click on some other link on the page without submitting , the the filled data should be stored in DB and when he gets back to the same page even after 2 days, he/she can retrieve the data which was filled earlier

For this purpose i have created a very small but usefull ClassLibrary , which works as follows …

(1) Create a new Class Library project in VS200* .

(2) Create a Method as follows

 public byte[] ObjectToByteArray(Object obj)

{ if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}

>

(3) Create table in your db and with 2 fields description(as string ) and Imagen(as image) type and write the method in your class lib project

public String InsertByteArrayIntoDB(String connString, String tableName, String dataID, byte[] byteArray)
{
SqlConnection conn = new SqlConnection(connString);
string query = "insert into " + tableName + "(DESCRIPCION, IMAGEN) " + "VALUES(@DESCRIPCION, @IMAGEN)";
SqlCommand cmd;

Now your object has been inserted into the DB, Lets see how to retrieve it from DB

try
{
conn.Open();
cmd = new SqlCommand(query, conn);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue(“Descripcion”, dataID).SqlDbType = SqlDbType.VarChar;
cmd.Parameters.AddWithValue(“Imagen”, byteArray).SqlDbType = SqlDbType.Image;
cmd.ExecuteNonQuery();
conn.Close();
return dataID;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

 

(4) write a method as:

public Object GetObjectFromDB(String connString, String tableName, String ID)
{
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand(); ;

 

try
{
cmd.CommandText = “select IMAGEN from ” + tableName + ” where DESCRIPCION = @Descripcion”;
cmd.Connection = conn;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue(“Descripcion”, ID).SqlDbType = SqlDbType.VarChar;
byte[] matriz = (byte[])cmd.ExecuteScalar();

BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(new MemoryStream(matriz));

// return matriz;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

the above method , return the Object , you can cast this object in your class object and can use it .

 

If you have any doubts in it , feel free to ask me about it.
Note: The class shoud be marked [Serializable], whose object is to be saved in to DB

Enjoy.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s