Manually create a table and add to a new dataset

  • Thread starter Thread starter Steve Wasser
  • Start date Start date
S

Steve Wasser

Can someone tell me if it is possible to manually create a DataSet (without
a Sqlconnection), then create a table via code and add that table to the
dataset? I'm trying to take a master table and split it into many different
tables based on a particular column value. Or, is there an easier
parent/child system I should use?

Thanks again!
 
Yes

There is a wizard to create a DataSet

SolutionExplore
<Your Project
Add Ne
Datase

You can then define tables and column definitions {XML elements} in the schema designer. Then you can generate a strongly typed dataset and insert rows into the tables..

--Richar
 
Steve,

Creating a new data table is as simple as:

DataTable pobjTable = new DataTable();

Of course, you have to define the columns on the table (adding through
the Columns property), and then add the rows (through the Rows property).
It would be a pain, but it can be done. You can then use it however you
want.

Hope this helps.
 
Hi Steve,

You can create a dataset and its tables from code, below you will find an
example of how to create two tables and a FK relationship.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


products = new DataSet("StaticTables");

#region The Category table
DataTable table = new DataTable("Category");
DataColumn pkCol = table.Columns.Add("CategoryID", typeof(int));
pkCol.AutoIncrementSeed=0;
pkCol.AutoIncrement = true;
table.Columns.Add("Name", typeof(string));
table.Columns.Add("ParentID", typeof(int));
table.Columns.Add("OriginalName", typeof(string));
table.PrimaryKey = new DataColumn[] {pkCol};
products.Tables.Add( table);
//Insert the default category
DataRow row = table.NewRow();
row["Name"]="";
row["ParentID"]=0;
row["OriginalName"]="";
table.Rows.Add( row);

#endregion

#region The Product Table
table = new DataTable("Product");
pkCol = table.Columns.Add("ProductID", typeof(int));
pkCol.AutoIncrement = true;
pkCol.AutoIncrementSeed = 1;
table.Columns.Add("Name", typeof(string));
table.Columns.Add("UPC", typeof(string));
table.Columns.Add("PriceLevel0", typeof(double));
table.Columns.Add("CategoryID", typeof(int));
table.Columns.Add("OnHand", typeof(double));
table.Columns.Add("Active", typeof(bool));
table.PrimaryKey = new DataColumn[] {pkCol};
products.Tables.Add( table);
#endregion

#region The Product Category Relationship
DataRelation fkRel = new DataRelation( "ProductCategory",
products.Tables["Category"].Columns["CategoryID"],
products.Tables["Product"].Columns["CategoryID"]);
products.Relations.Add( fkRel);
#endregion
 
Right, I've already created the new table, is it even associated with a
dataset? Can I associate it? Would it be pointless?

Thanks

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
Nicholas Paldino said:
Steve,

Creating a new data table is as simple as:

DataTable pobjTable = new DataTable();

Of course, you have to define the columns on the table (adding through
the Columns property), and then add the rows (through the Rows property).
It would be a pain, but it can be done. You can then use it however you
want.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve Wasser said:
Can someone tell me if it is possible to manually create a DataSet (without
a Sqlconnection), then create a table via code and add that table to the
dataset? I'm trying to take a master table and split it into many different
tables based on a particular column value. Or, is there an easier
parent/child system I should use?

Thanks again!
 
Thank you! That's exactly what I was looking for. I just want a sensible
container to add these new tables into rather than having them float all
around.

Thanks,

Steve

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
Ignacio Machin ( .NET/ C# MVP ) said:
Hi Steve,

You can create a dataset and its tables from code, below you will find an
example of how to create two tables and a FK relationship.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


products = new DataSet("StaticTables");

#region The Category table
DataTable table = new DataTable("Category");
DataColumn pkCol = table.Columns.Add("CategoryID", typeof(int));
pkCol.AutoIncrementSeed=0;
pkCol.AutoIncrement = true;
table.Columns.Add("Name", typeof(string));
table.Columns.Add("ParentID", typeof(int));
table.Columns.Add("OriginalName", typeof(string));
table.PrimaryKey = new DataColumn[] {pkCol};
products.Tables.Add( table);
//Insert the default category
DataRow row = table.NewRow();
row["Name"]="";
row["ParentID"]=0;
row["OriginalName"]="";
table.Rows.Add( row);

#endregion

#region The Product Table
table = new DataTable("Product");
pkCol = table.Columns.Add("ProductID", typeof(int));
pkCol.AutoIncrement = true;
pkCol.AutoIncrementSeed = 1;
table.Columns.Add("Name", typeof(string));
table.Columns.Add("UPC", typeof(string));
table.Columns.Add("PriceLevel0", typeof(double));
table.Columns.Add("CategoryID", typeof(int));
table.Columns.Add("OnHand", typeof(double));
table.Columns.Add("Active", typeof(bool));
table.PrimaryKey = new DataColumn[] {pkCol};
products.Tables.Add( table);
#endregion

#region The Product Category Relationship
DataRelation fkRel = new DataRelation( "ProductCategory",
products.Tables["Category"].Columns["CategoryID"],
products.Tables["Product"].Columns["CategoryID"]);
products.Relations.Add( fkRel);
#endregion



Steve Wasser said:
Can someone tell me if it is possible to manually create a DataSet (without
a Sqlconnection), then create a table via code and add that table to the
dataset? I'm trying to take a master table and split it into many different
tables based on a particular column value. Or, is there an easier
parent/child system I should use?

Thanks again!
 
Back
Top