.comment-link {margin-left:.6em;}
Marc Boizeau's blog
Wednesday, September 21, 2005
  Context saving with persistent datasets
This article propose a way to use dataset to make persistent an arbitrary set of data.in the context of a winform application

Sometimes you need to stored datas but you don't think a database is the right way.
For instance if you want to store the user's context (search criterias,options, the last time he use the application...) in order to set it back the next time the application is used, you need a specific tool.
To achieve this you may use a little xml file.
But, most of the time, you don't know exactly what you will need to store in advance.
So defining and changing and changing again the schema of the xml file could be something close to a good headache.

An idea is to propose a set of methods which adapt the schema inline.

The microsoft .Net DataSet object has two powerfull methods ReadXml and WriteXml.
These methods manipulates the datas and also the schema of the dataset.
So I choose this object to implement my context saving class for winform application.
(It could be easily adapt for a server side use)

The idea is that when a coder need a new persistent data, he use the following syntax:

Contexte.getSimpleval("MyDataName");
The schema will be changed at the first use and the next time, it will bring back the last setled value.

When modified, the data is stored like this:
Contexte.setSimpleval("MyNewinformationName", MyStringValue);// the stored info are here of string type

If the amount of information is important data could be sort by collections using the syntaxes:

getVal(string "MyNewCollectionName","MyNewinformationName")
and
setVal(string "MyNewCollectionName","MyNewinformationName", MyStringValue)

The "save()" and "load()" methods are called at the begining and at the end of the application.
(typically the onLoad and onClose events of the main window).

Here is the code of the class, please critisize, comment and ask questions!


using System;
using System.Data;
using System.IO;

namespace MyNamespace
{
/// <summary>
/// The Contexte class is used to make datas persistant
/// Marc Boizeau
/// Two types of data are managed
/// - collections: values identified by a collection name and a field name
/// - Simple values just identified by a name
/// </summary>
public class Contexte
{
private const string FILE_NAME="Context.xml";
// context data container dataset
static private DataSet ds;

public Contexte()
{
}

/// <summary>
/// Retrives the good path for context
/// </summary>
/// <returns> the context path, string </returns>
static private string getFilePath()
{
//to change
string path= Environment.GetEnvironmentVariable("APPDATA")+"/MyApplication/";

if (!Directory.Exists(path))
Directory.CreateDirectory(path);


path+=FILE_NAME;

return path ;

}
//load the dataset with the xml file
public static void load()
{
if (ds!=null)
ds = null;

ds = new DataSet("ContexteExpertises");

try
{
if (File.Exists(getFilePath()))
ds.ReadXml( getFilePath(),XmlReadMode.ReadSchema);
}
catch(Exception e)
{// files do
}

}

//save the datafile
public static void Save()
{
if(ds== null)
{
ds =new DataSet("ContexteExpertises");
}
ds.WriteXml(getFilePath(),XmlWriteMode.WriteSchema);
}

// retrieve a datatable by its name
private static DataTable getTable(string TableName)
{
DataTable dt;
if (!ds.Tables.Contains(TableName))
{
dt = new DataTable(TableName);
dt.Columns.Add("NAME");
dt.Columns.Add("VALUE");
dt.Constraints.Add("PK",dt.Columns["NAME"],true);
ds.Tables.Add(dt);
}
else
dt = ds.Tables[TableName];
return dt;
}

//retrieves a data by a table name and a field name
public static string getVal(string TableName, string Entry)
{
DataTable dt;
DataView dv;
dt = getTable(TableName);
dv = dt.DefaultView;

if (Entry==null)
Entry="";

dv.RowFilter= " NAME ='" + Entry+ "'";
//test if exists
if (dv.Count==0)
{//if not creates
DataRow dr =dt.NewRow();
dr["NAME"]= Entry;
dr["VALUE"]="";
dt.Rows.Add(dr);
}
//at this point the entry exist
return dt.Rows.Find(Entry)["VALUE"].ToString();
}


// setting a data
public static void setVal(string TableName, string Entry,string Val)
{
DataTable dt;
DataView dv;
dt = getTable(TableName);
dv = dt.DefaultView;
dv.RowFilter= " NAME ='" + Entry+ "'";

if (Entry==null)
Entry="";

if (Val==null)
Val="";

//test if exists
if (dv.Count==0)
{//if not creates
DataRow dr =dt.NewRow();
dr["NAME"]= Entry;
dr["VALUE"]= "";
dt.Rows.Add(dr);
}
//at this point the entry exists
dt.Rows.Find(Entry)["VALUE"]=Val;
}

/// <summary>
/// retrieves a simple value
/// </summary>
/// <param name="Entry"></param>
/// <returns></returns>
public static string getSimpleVal(string Entry)
{
if (ds==null)
load();
return getVal("SIMPLE", Entry);
}

/// <summary>
/// set a simple value
/// </summary>
/// <param name="Entry"></param>
/// <returns></returns>
public static void setSimpleVal(string Entry,string val)
{
if (ds==null)
load();
setVal("SIMPLE", Entry,val);
}



}
}
 
Comments: Post a Comment



<< Home
You are a developer and work with Oracle and Microsoft technologies? Have a look!
ATOM
How to:
Use updatable views in Access
Get data in Excel from Oracle 1
Get data in Excel from Oracle 2
Draw the Mandelbrot set using C#
Use the "Grouping Sets" SQl Syntax
Use the "Rollup" SQl Syntax
Use the "Rank over" SQl Syntax

.net webservice session
Winform, Web Services & credential
back to work
self description
A web crawler(regular expressions and httprequest)
call an Oracle stored proc with oledb .net
Updating two lines with one data adapter.
oracle functions
link
Quotes & parameters
ARCHIVES
October 2004 / November 2004 / December 2004 / January 2005 / February 2005 / March 2005 / April 2005 / June 2005 / August 2005 / September 2005 / December 2005 / February 2006 / December 2006 / March 2009 /


Powered by Blogger

mboizeau.free.fr