Building a DataSet without a Database

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I have several pieces of information (about 20 rows) that I would like to
put in a DataSet to make them easier to use in my application. Because it is
very little data, I would prefer not to make a database for such little data
that will very rarely be changed. However, everything I have been able to
find only uses DataSets for connecting to databases. Is it possible to put
data into a DataSet without using a database? Any help would be appreciated.
Thanks.
 
why dont you create a small XML file and bind to it automatically as you
would a database?

Alternatively, to build your own from your data use the code below, dont
forget to look into caching to get the best advantags of data rarely
changing

private DataTable CreateDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add("LastName", Type.GetType("System.String"));
dt.Rows.Add(new object[] {"Smith"});
dt.Rows.Add(new object[] {"Jones"});
dt.Rows.Add(new object[] {"Clark"});
return dt;
}

private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
DataList1.AlternatingItemTemplate =
Page.LoadTemplate("NewTemplate.ascx");
}

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
DataList1.DataSource = CreateDataSource();
DataList1.DataBind();
}
}

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
A DataSet doesn't actually hold any data directly. Instead, a DataSet has a
collection of DataTable objects. These DataTable objects, in turn, contain
a collection of DataColumns. After a DataTable is configured with the
columns it will need, you can add a DataRow to the Rows collection of the
DataTable.

You need not have a database, but you will need to manually create the Table
and its columns and rows so that you can put this table in the tables
collection of a DataSet.

Try looking at help on creating DataTables or DataColumns. This should show
you what you need to do.
 
Nathan,

Typed here in this message (so watch typos), takings John's sample combined
with Scotts text, than you get something as.

private void CreateDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("LastName", Type.GetType("System.String"));
dt.Rows.Add(new object[] {"Smith"});
dt.Rows.Add(new object[] {"Jones"});
dt.Rows.Add(new object[] {"Clark"});
ds.Tables.Add(dt);
ds.AcceptChanges();
ds.WriteXml("OnMyPath");
}
private void WriteDataSet(string MyText)
{
ds.ReadXML("OnMyPath")
ds.Tables[0].Rows[0]["LastName"] = MyText;
ds.WriteXML("OnMyPath");
}
Be aware to set a very good routine around that writing of the file. When
you write it and have an error, then you will loose everything, when you
close the program without correcting it.

(When you don't understand this C# than reply, than I do it in VBNet,
however tell that next time please in your message)

I hope this helps,

Cor
 
Back
Top