Create new typed DataSet object (c#)
I use a DataGrid to show a xml file. The Grid's DataSource is a Typed DataSet.(using schema)
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("XML_Reader.Resources.schema.xsd");
XmlSchemaSet schemas = new XmlSchemaSet();
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, XmlReader.Create(stream));
using (XmlReader reader = XmlReader.Create(xmlFile, settings))
{
newDataSet.ReadXm开发者_开发技巧l(reader);
}
dataGrid.DataSource = newDataSet;
I added a xsd schema to my project and used MSDataSetGenerator to generate the newDataSet. (VS2008). Now i want to create a new DataSet object for every new (hierarchical xml) file i read.Creating a new DataSet object isn't a problem but the data types aren 't correct, so i can't sort them well (specifically the numerical fields). In my view, i need to create a new Typed DataSet. So how can i fix this ?Datasets in Visual Studio Overview -
Typed Versus Untyped Datasets
A typed dataset is a dataset that is first derived from the base DataSet class and then uses information from the Dataset Designer, which is stored in an .xsd file, to generate a new strongly-typed dataset class. Information from the schema (tables, columns, and so on) is generated and compiled into this new dataset class as a set of first-class objects and properties. Because a typed dataset inherits from the base DataSet class, the typed class assumes all of the functionality of the DataSet class and can be used with methods that take an instance of a DataSet class as a parameter.
An untyped dataset, in contrast, has no corresponding built-in schema. As in a typed dataset, an untyped dataset contains tables, columns, and so on — but those are exposed only as collections. (However, after manually creating the tables and other data elements in an untyped dataset, you can export the dataset's structure as a schema using the dataset's WriteXmlSchema method.)
Yes you can automatically generate these with Visual Studio:
- How to: Create a Typed Dataset
Or, you can create your own strongly typed DataSets (this is cleaner IMO). Example:
using System.Data;
public class CatsDataTable : DataTable
{
public CatsDataTable() : base()
{
base.TableName = "cats";
Columns.Add(new DataColumn(SqlTokens.id_cats, typeof(int)));
Columns.Add(new DataColumn(SqlTokens.owners_id_owners, typeof(int)));
Columns.Add(new DataColumn(SqlTokens.cats_name, typeof(string)));
Columns.Add(new DataColumn(SqlTokens.cats_number_of_spots, typeof(int)));
}
}
public class OwnersDataTable : DataTable
{
public OwnersDataTable() : base()
{
base.TableName = "owners";
Columns.Add(new DataColumn(SqlTokens.id_owners, typeof(int)));
Columns.Add(new DataColumn(SqlTokens.owners_name, typeof(string)));
}
}
public class PetsDataSet : DataSet
{
public PetsDataSet() : base()
{
base.TableName = "pets";
Tables.Add("cats");
Tables.Add("owners");
}
}
Tangent about Garbage Collection
garbage collection (GC) is a form of automatic memory management. It is a special case of resource management, in which the limited resource being managed is memory. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. (wikipedia)
- Garbage collection (wikipedia)
- Garbage Collector Basics and Performance Hints
Let me answer my own question ;-))
A typed DataSet is simply a class you can instantiate like any other class.There is no magic to anything generated by tools, those tools simply generate classes and you can use those classes the same way you use other classes.
Do NewDataSet d1 = new NewDataSet();
where you put the right class name there instead of "NewDataSet".
精彩评论