HighTechTalks DotNet Forums  

What sort of dataset is it?

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss What sort of dataset is it? in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
=?Utf-8?B?RGF2aWQ=?=
 
Posts: n/a

Default What sort of dataset is it? - 08-17-2007 , 11:36 AM






I have an application that saves a bunch of data in XML files that come from
typed datasets.

So, I have something like this:

MyTypedDs myds;

//.... When the user wants to save the data

myds.WriteXML(thefilename,XMLWriteMode.WriteSchema );

Later on, when he wants to open a saved file,

myds=new (MyTypedDs);
myds.ReadXML(thefilename,XMLReadMode.ReadSchema);



Great. So far, so good.

Now, some fields have been added to the records. The tables in the dataset
have some additional columns. I want to be able to open up the old files
that have been previously saved, and if the file is from the old format,
convert it to the new format, filling in default values for missing fields.

TypedDSV1 myversion1ds;
TypedDSV2 myversion2ds;

DataSet myds;

//When opening a file.

myds.ReadXML(thefilename,XMLReadMode.ReadSchema)
if (the schema from the file matches version1)
{myversion1ds=(TypedDSV1)myds;
Convertv1tov2(myversion1ds,myversion2ds);
}
else if (the schema from the file matches version 2)
{
myversion2ds=(TypedDSV2)myds;
}
else return retvals.Schemadoesntmatch;

So, is there a very easy way to write the (the schema from the file matches
version 2) function?

Reply With Quote
  #2  
Old   
=?Utf-8?B?TWFuaXNoIEJhZm5h?=
 
Posts: n/a

Default RE: What sort of dataset is it? - 08-17-2007 , 01:38 PM






Hi,
you can try something like this in below sample code:
private void Form1_Load(object sender, EventArgs e)
{

DataTable dt = new DataTable();
DataColumn col1 = new DataColumn();
DataColumn col2 = new DataColumn();
col1.ColumnName = "Name";
col2.ColumnName = "PostingTime";

col1.DataType = typeof(System.String);
col2.DataType = typeof(System.DateTime);
dt.Columns.Add(col1);
dt.Columns.Add(col2);

DataRow row1 = dt.NewRow();
row1["Name"] = "Manish Bafna";
row1["PostingTime"] = DateTime.Now;

DataRow row2 = dt.NewRow();
row2["Name"] = "Sanjay Bafna";
row2["PostingTime"] = DateTime.Now.AddHours(5);

dt.Rows.Add(row1);
dt.Rows.Add(row2);

dt.AcceptChanges();

DataSet ds = new DataSet();
ds.Tables.Add(dt);

ds.WriteXml("XMLFile1.xml", XmlWriteMode.IgnoreSchema);

dataGridView1.DataSource = dt;

DataColumn col3 = new DataColumn();
col3.ColumnName = "NewColumn";
col3.DataType = typeof(System.String);
col3.DefaultValue = "Hello";
ds.Tables[0].Columns.Add(col3);
DataRow row3 = ds.Tables[0].NewRow();

row3["Name"] = "Janak NAIK";
row3["PostingTime"] = DateTime.Now.AddYears(2);
row3["NewColumn"] = "Welcome";
ds.Tables[0].Rows.Add(row3);

ds.AcceptChanges();

DataSet ds2 = new DataSet();

ds2.ReadXml("XMLFile1.xml", XmlReadMode.IgnoreSchema);

ds.Merge(ds2, true, MissingSchemaAction.Ignore);

dataGridView2.DataSource = ds.Tables[0];
}

--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



"David" wrote:

Quote:
I have an application that saves a bunch of data in XML files that come from
typed datasets.

So, I have something like this:

MyTypedDs myds;

//.... When the user wants to save the data

myds.WriteXML(thefilename,XMLWriteMode.WriteSchema );

Later on, when he wants to open a saved file,

myds=new (MyTypedDs);
myds.ReadXML(thefilename,XMLReadMode.ReadSchema);



Great. So far, so good.

Now, some fields have been added to the records. The tables in the dataset
have some additional columns. I want to be able to open up the old files
that have been previously saved, and if the file is from the old format,
convert it to the new format, filling in default values for missing fields.

TypedDSV1 myversion1ds;
TypedDSV2 myversion2ds;

DataSet myds;

//When opening a file.

myds.ReadXML(thefilename,XMLReadMode.ReadSchema)
if (the schema from the file matches version1)
{myversion1ds=(TypedDSV1)myds;
Convertv1tov2(myversion1ds,myversion2ds);
}
else if (the schema from the file matches version 2)
{
myversion2ds=(TypedDSV2)myds;
}
else return retvals.Schemadoesntmatch;

So, is there a very easy way to write the (the schema from the file matches
version 2) function?

Reply With Quote
  #3  
Old   
=?Utf-8?B?RGF2aWQ=?=
 
Posts: n/a

Default RE: What sort of dataset is it? - 08-21-2007 , 02:26 PM



Thanks, although I was kind of hoping for a "compareschema" function. It
would seem useful. I wonder why it doesn't exist.

I think that, instead, what I really need is to see if the two datasets have
the same number of tables and same columns. So, I think I'll write a utility
that compares the columnames in each table.

"Manish Bafna" wrote:

Quote:
Hi,
you can try something like this in below sample code:
private void Form1_Load(object sender, EventArgs e)
{

DataTable dt = new DataTable();
DataColumn col1 = new DataColumn();
DataColumn col2 = new DataColumn();
col1.ColumnName = "Name";
col2.ColumnName = "PostingTime";

col1.DataType = typeof(System.String);
col2.DataType = typeof(System.DateTime);
dt.Columns.Add(col1);
dt.Columns.Add(col2);

DataRow row1 = dt.NewRow();
row1["Name"] = "Manish Bafna";
row1["PostingTime"] = DateTime.Now;

DataRow row2 = dt.NewRow();
row2["Name"] = "Sanjay Bafna";
row2["PostingTime"] = DateTime.Now.AddHours(5);

dt.Rows.Add(row1);
dt.Rows.Add(row2);

dt.AcceptChanges();

DataSet ds = new DataSet();
ds.Tables.Add(dt);

ds.WriteXml("XMLFile1.xml", XmlWriteMode.IgnoreSchema);

dataGridView1.DataSource = dt;

DataColumn col3 = new DataColumn();
col3.ColumnName = "NewColumn";
col3.DataType = typeof(System.String);
col3.DefaultValue = "Hello";
ds.Tables[0].Columns.Add(col3);
DataRow row3 = ds.Tables[0].NewRow();

row3["Name"] = "Janak NAIK";
row3["PostingTime"] = DateTime.Now.AddYears(2);
row3["NewColumn"] = "Welcome";
ds.Tables[0].Rows.Add(row3);

ds.AcceptChanges();

DataSet ds2 = new DataSet();

ds2.ReadXml("XMLFile1.xml", XmlReadMode.IgnoreSchema);

ds.Merge(ds2, true, MissingSchemaAction.Ignore);

dataGridView2.DataSource = ds.Tables[0];
}

--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



"David" wrote:

I have an application that saves a bunch of data in XML files that come from
typed datasets.

So, I have something like this:

MyTypedDs myds;

//.... When the user wants to save the data

myds.WriteXML(thefilename,XMLWriteMode.WriteSchema );

Later on, when he wants to open a saved file,

myds=new (MyTypedDs);
myds.ReadXML(thefilename,XMLReadMode.ReadSchema);



Great. So far, so good.

Now, some fields have been added to the records. The tables in the dataset
have some additional columns. I want to be able to open up the old files
that have been previously saved, and if the file is from the old format,
convert it to the new format, filling in default values for missing fields.

TypedDSV1 myversion1ds;
TypedDSV2 myversion2ds;

DataSet myds;

//When opening a file.

myds.ReadXML(thefilename,XMLReadMode.ReadSchema)
if (the schema from the file matches version1)
{myversion1ds=(TypedDSV1)myds;
Convertv1tov2(myversion1ds,myversion2ds);
}
else if (the schema from the file matches version 2)
{
myversion2ds=(TypedDSV2)myds;
}
else return retvals.Schemadoesntmatch;

So, is there a very easy way to write the (the schema from the file matches
version 2) function?

Reply With Quote
  #4  
Old   
=?Utf-8?B?TWFuaXNoIEJhZm5h?=
 
Posts: n/a

Default RE: What sort of dataset is it? - 08-21-2007 , 09:42 PM



Hi,
I think i have gone little bit off the topic.You can use use in-built
GetSchema method of DataTable,Dataset or connection in your custom
compareschema method.Google for Getschema and you will get further insights
on how you can use in your compareschema method.It can be as simple as
something like this:
If dataset1.GetSchema().Equals(dataset2.Getschema()
{
messagebox.show("Schema are equal");
}
else
{
messagebox.show("Schema are not equal");
}
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



"David" wrote:

Quote:
Thanks, although I was kind of hoping for a "compareschema" function. It
would seem useful. I wonder why it doesn't exist.

I think that, instead, what I really need is to see if the two datasets have
the same number of tables and same columns. So, I think I'll write a utility
that compares the columnames in each table.

"Manish Bafna" wrote:

Hi,
you can try something like this in below sample code:
private void Form1_Load(object sender, EventArgs e)
{

DataTable dt = new DataTable();
DataColumn col1 = new DataColumn();
DataColumn col2 = new DataColumn();
col1.ColumnName = "Name";
col2.ColumnName = "PostingTime";

col1.DataType = typeof(System.String);
col2.DataType = typeof(System.DateTime);
dt.Columns.Add(col1);
dt.Columns.Add(col2);

DataRow row1 = dt.NewRow();
row1["Name"] = "Manish Bafna";
row1["PostingTime"] = DateTime.Now;

DataRow row2 = dt.NewRow();
row2["Name"] = "Sanjay Bafna";
row2["PostingTime"] = DateTime.Now.AddHours(5);

dt.Rows.Add(row1);
dt.Rows.Add(row2);

dt.AcceptChanges();

DataSet ds = new DataSet();
ds.Tables.Add(dt);

ds.WriteXml("XMLFile1.xml", XmlWriteMode.IgnoreSchema);

dataGridView1.DataSource = dt;

DataColumn col3 = new DataColumn();
col3.ColumnName = "NewColumn";
col3.DataType = typeof(System.String);
col3.DefaultValue = "Hello";
ds.Tables[0].Columns.Add(col3);
DataRow row3 = ds.Tables[0].NewRow();

row3["Name"] = "Janak NAIK";
row3["PostingTime"] = DateTime.Now.AddYears(2);
row3["NewColumn"] = "Welcome";
ds.Tables[0].Rows.Add(row3);

ds.AcceptChanges();

DataSet ds2 = new DataSet();

ds2.ReadXml("XMLFile1.xml", XmlReadMode.IgnoreSchema);

ds.Merge(ds2, true, MissingSchemaAction.Ignore);

dataGridView2.DataSource = ds.Tables[0];
}

--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



"David" wrote:

I have an application that saves a bunch of data in XML files that come from
typed datasets.

So, I have something like this:

MyTypedDs myds;

//.... When the user wants to save the data

myds.WriteXML(thefilename,XMLWriteMode.WriteSchema );

Later on, when he wants to open a saved file,

myds=new (MyTypedDs);
myds.ReadXML(thefilename,XMLReadMode.ReadSchema);



Great. So far, so good.

Now, some fields have been added to the records. The tables in the dataset
have some additional columns. I want to be able to open up the old files
that have been previously saved, and if the file is from the old format,
convert it to the new format, filling in default values for missing fields.

TypedDSV1 myversion1ds;
TypedDSV2 myversion2ds;

DataSet myds;

//When opening a file.

myds.ReadXML(thefilename,XMLReadMode.ReadSchema)
if (the schema from the file matches version1)
{myversion1ds=(TypedDSV1)myds;
Convertv1tov2(myversion1ds,myversion2ds);
}
else if (the schema from the file matches version 2)
{
myversion2ds=(TypedDSV2)myds;
}
else return retvals.Schemadoesntmatch;

So, is there a very easy way to write the (the schema from the file matches
version 2) function?

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.