InMemory DataSet

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

I want to create an In Memory dataset. not connected to any database.. but
putting my own info in from code or a file....
What are the steps to do this? Where can I find some info on how to do
this?
Brian
 
I want to create an In Memory dataset.  not connected to any database..but
putting my own info in from code or a file....
What are the steps to do this?  Where can I find some info on how to do
this?
      Brian

Dim dataSet As New DataSet()

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
All DataSets are in-memory and disconnected from any data source.

You should simply Google on .NET DataSet and read the many usefull articles
about them.

-Scott
 
lol...
ok.. a little more info...
I want to create a table with a few columns... and a few rows....

I want to create an In Memory dataset. not connected to any database.. but
putting my own info in from code or a file....
What are the steps to do this? Where can I find some info on how to do
this?
Brian

Dim dataSet As New DataSet()

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
ok... how do i make bind that with a dataview?...
here are the crazy things I've been trying
Public Class Form1

Private MyDS As DataSet = New DataSet

Private MyTable As DataTable = MyDS.Tables.Add("tblHolidays") 'New
DataTable("tblHolidays")

Private Sub testme()

'Dim dt As New DataTable("tblHolidays")

Dim dcfirstcolumn As New DataColumn("first")

Dim dcsecondcolumn As New DataColumn("second")

dcfirstcolumn.DataType = System.Type.GetType("System.DateTime")

dcsecondcolumn.DataType = System.Type.GetType("System.String")

MyTable.Columns.Add(dcfirstcolumn)

MyTable.Columns.Add(dcsecondcolumn)

Dim dr As DataRow = MyTable.NewRow() '= dt.Rows.Add ' 'dt.rows.newrow

MyTable.Rows.Add(dr)

dr.Item(dcfirstcolumn) = #1/1/2008#

dr.Item(dcsecondcolumn) = "New Years"

dr.AcceptChanges()

'MyTable.Rows(1).Item(0) = #1/1/2009#

' dr.

Me.DataGridView1.DataSource = Me.MyDS.Tables

MyDS.AcceptChanges()

'Me.DataGridView1.DataBindings



End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'With MyDS

' With MyTable

' .Columns.Add("dtmHoliday", Type.GetType("System.DateTime"))

' .Columns.Add("strHoliday", Type.GetType("System.String"))

' .Columns(1).ColumnName = "test"

' .Columns(0).Caption = "test caption"

' End With

' Console.WriteLine("Rows: {0}", .Tables(0).Rows.Count)

' MyTable.Rows.Add()

' Dim strSQL As String = "INSERT INTO tblHolidays (dtmHoliday, strHoliday)
values (#1/1/2009#, 'Testme')"

' Dim selectCMD As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL)

' 'selectCMD.ExecuteScalar()

' Console.WriteLine("Rows: {0}", .Tables(0).Rows.Count)

' .AcceptChanges()

Dim DS As New DataSet("MyDataSet")

Dim DT As New DataTable("MyTable")

DT.Columns.Add("Test", GetType(String))

Dim DR As DataRow = DT.NewRow

DR.Item("Test") = "Hello, World"

DT.Rows.Add(DR)

DS.Tables.Add(DT)

DS.AcceptChanges()

Me.DataGridView1.DataSource = DS.Tables

'End With

'testme()

End Sub

End Class
 
Brian,

You use the code to set the datasource of a datagrid, that can be a dataset.

A dataGridView only accept one table. Probably the DataGrid was to complex
and therefore Microsoft has introduced an in fact more simple "Data" grid.

By the way, be aware that the "acceptchanges" is not really needed in the
sample from Steve.
It can be done, does not harm, but that depends if you want to keep the
adding of the start row as new row ar as begin row.

I also would not use the literal dates. It is from the time that Microsoft
was probably thinking that they were a local player on the markt and
therefore only really suitable in the USA.

After a lot of discussions in past in this newsgroup, we came with this as
the best solution to initiate a date.

dim mydate as datetime = new datetime(2008, 1, 1)
giving as result in every place on earth the first day of january of the
year 2008.

Cor
 
There is another middle ground approach.

Create a strongl (-ly typed) dataset. And do it in memory.

Here is a crappy example I just wrote up for another person.

Sorry for the c# syntax, it'll convert easy.


Create a new strongly typed DataSet. Call it "StateStuffDS" (or whatever
you want to call it, try to avoid ambiguity)

Add 3 tables (with the columns)

State
StateID (int)
StateName (string)
StateAbbreviation (string)

StateRule
StateRuleID (int)
StateRuleText (string)


StateToStateRule
StateID (int)
StateRuleID (int)




Here is some psedu code.


StateStuffDS ds = new StateStuffDS();

ds.State.AddNewStateRow( 101, "Virginia", "VA");
ds.StateRule.AddNewStateRule ( 1001, "Is For Lovers" );
ds.StateToStateRule.AddStateToStateRule( 101, 1001);

ds.WriteXml (@"C:\myds.xml");


.........................

StateStuffDS anotherDS = new StateStuffDS();
anotherDS.Read("C:\myds.xml");
DataRow [] foundRows = ds.State.Select("StateAbbreviation = 'VA'");
//include the System.Data namespace

if(foundRows.Length > 0) //or is it .Count?
{
StateStuffDS.State.StateRow currentRow = foundRows[0] as
StateStuffDS.State.StateRow; //just a test to pop off the first row
if(null!= currentRow)
{
Console.Writeline (currentRow.StateName);
}
}



You get some strong typing this way, even in the DataSet model.






Good luck!
 
sloan,

A strongly typed dataset is normally inheriting the dataset class, I miss
that in your code.

Any idea what I miss?

Cor

sloan said:
There is another middle ground approach.

Create a strongl (-ly typed) dataset. And do it in memory.

Here is a crappy example I just wrote up for another person.

Sorry for the c# syntax, it'll convert easy.


Create a new strongly typed DataSet. Call it "StateStuffDS" (or whatever
you want to call it, try to avoid ambiguity)

Add 3 tables (with the columns)

State
StateID (int)
StateName (string)
StateAbbreviation (string)

StateRule
StateRuleID (int)
StateRuleText (string)


StateToStateRule
StateID (int)
StateRuleID (int)




Here is some psedu code.


StateStuffDS ds = new StateStuffDS();

ds.State.AddNewStateRow( 101, "Virginia", "VA");
ds.StateRule.AddNewStateRule ( 1001, "Is For Lovers" );
ds.StateToStateRule.AddStateToStateRule( 101, 1001);

ds.WriteXml (@"C:\myds.xml");


........................

StateStuffDS anotherDS = new StateStuffDS();
anotherDS.Read("C:\myds.xml");
DataRow [] foundRows = ds.State.Select("StateAbbreviation = 'VA'");
//include the System.Data namespace

if(foundRows.Length > 0) //or is it .Count?
{
StateStuffDS.State.StateRow currentRow = foundRows[0] as
StateStuffDS.State.StateRow; //just a test to pop off the first row
if(null!= currentRow)
{
Console.Writeline (currentRow.StateName);
}
}



You get some strong typing this way, even in the DataSet model.






Good luck!




Brian said:
I want to create an In Memory dataset. not connected to any database..
but putting my own info in from code or a file....
What are the steps to do this? Where can I find some info on how to do
this?
Brian
 
I think my syntax was el-crappo.

Here is some rewording:
Create a strongl (-ly typed) dataset (through the IDE)
<<strike out>>And do it in memory.<</strike out>>
<<new sentence>>And then you will have the ability to create your dataset in
memory, but at the same time, you'll have access to "strong" typing,
intellisense, etc.
<</new sentence>>
.............

When you go through the IDE and "add new item/ dataset", you are basically
creating a object that will inherit from DataSet.
However, all your tables and columns will be "pre-defined", thus you won't
have to write code like this:

Dim DS As New DataSet("MyDataSet")
Dim DT As New DataTable("MyTable")
DT.Columns.Add("Test", GetType(String))


......
I am NOT saying there is never a time to write code like that. I'm saying
there is another option.
If you're entities are fairly stable, then I prefer the strong dataset
approach over a loose dataset approach.







Cor Ligthert said:
sloan,

A strongly typed dataset is normally inheriting the dataset class, I miss
that in your code.

Any idea what I miss?

Cor

sloan said:
There is another middle ground approach.

Create a strongl (-ly typed) dataset. And do it in memory.

Here is a crappy example I just wrote up for another person.

Sorry for the c# syntax, it'll convert easy.


Create a new strongly typed DataSet. Call it "StateStuffDS" (or
whatever
you want to call it, try to avoid ambiguity)

Add 3 tables (with the columns)

State
StateID (int)
StateName (string)
StateAbbreviation (string)

StateRule
StateRuleID (int)
StateRuleText (string)


StateToStateRule
StateID (int)
StateRuleID (int)




Here is some psedu code.


StateStuffDS ds = new StateStuffDS();

ds.State.AddNewStateRow( 101, "Virginia", "VA");
ds.StateRule.AddNewStateRule ( 1001, "Is For Lovers" );
ds.StateToStateRule.AddStateToStateRule( 101, 1001);

ds.WriteXml (@"C:\myds.xml");


........................

StateStuffDS anotherDS = new StateStuffDS();
anotherDS.Read("C:\myds.xml");
DataRow [] foundRows = ds.State.Select("StateAbbreviation = 'VA'");
//include the System.Data namespace

if(foundRows.Length > 0) //or is it .Count?
{
StateStuffDS.State.StateRow currentRow = foundRows[0] as
StateStuffDS.State.StateRow; //just a test to pop off the first row
if(null!= currentRow)
{
Console.Writeline (currentRow.StateName);
}
}



You get some strong typing this way, even in the DataSet model.






Good luck!




Brian said:
I want to create an In Memory dataset. not connected to any database..
but putting my own info in from code or a file....
What are the steps to do this? Where can I find some info on how to do
this?
Brian
 
.....
I am NOT saying there is never a time to write code like that. I'm saying
there is another option.
If you're entities are fairly stable, then I prefer the strong dataset
approach over a loose dataset approach.
me too.

:-)

Cor
 
silly me.. found what I was doing wrong.... had bound to a blank table
through the gui.....
 
Back
Top