mysteries of instantiation

D

dennist

I have a good friend named Steve Wiesner, a quantum
physicist. You can read about him in The Code Book, in
which he's described as the father of quantum
cryptology. I've acquired a layman's understanding of
what Einstein called "spooky action at a distance."

However, quantum entanglement is child's play compared to
instantiation of datasets.

Here's a bit of code that works, although I don't know
why.

NewTopic = False


Try
cn.Open()
Catch er As Exception
MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
End Try



'Dim da As OleDbDataAdapter = New OleDbDataAdapter
'Dim ds As New DataSet
'ds.DataSetName = "ds1"
'cn.Open()
'da.FillSchema(ds, SchemaType.Source, "Topics")
'''cn.Close()
'ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\H:\HasbaraNET\ado.net tests\Kevin
corrections\TFSNet2\TFSNet2\ds.xsd")


Dim cmd As New OleDbCommand("SELECT * FROM
Topics", cn)
Dim da As OleDbDataAdapter = New OleDbDataAdapter
da.SelectCommand = cmd
' Fill the DataSet
Dim ds As DataSet = New DataSet
ds.DataSetName = "ds1"
da.FillSchema(ds, SchemaType.Source, "Topics")
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\Kevin corrections\TFSNet2\TFSNet2\ds.xsd")

Dim dsA As New ds1

da.Fill(dsA, "Topics")

First, note that the dataset is ds, and the name of the
dataset is ds1. When I make this a strongly typed
dataset in code, I don't instantiate ds, I just use it,
ending up with ds.xsd(and going through the awkward
xsd.exe in console mode).

However, in order to use the dataset, I don't instantiate
it. I don't, for example, dim dsA as new ds. I
instantiae its NAME. If I try to instantiate the actual
dataset, I get an error.

If I don't have to instantiate it at all to create a
strongly typed dataset in code, why can't I just use it,
e.g. da.fill(ds, "Topics)? Even more bizarre, why do I
have to instantiate it's NAME, and fill it's NAME, not
the actual dataset.

Nobel prize winning quantum physicist Richard Feynman
said "Nobody really understands quantum mechanics."

Well, I don't really understand instantiation
requirements for datasets. Can somebody explain to me
what's really going on here, so when I do it I'm not just
doing it by rote?

thank you.

dennist
 
Y

Yan-Hong Huang[MSFT]

Hello Dennist,

Thanks for posting in the group.

Based on my experience, the following code should work fine:

daAuthors.FillSchema(dsPubs, SchemaType.Source, "Authors")
daAuthors.Fill(dsPubs, "Authors")

The DataAdapter class provides two methods, Fill and FillSchema, that are
crucial to loading this data. Both of these methods load information into a
DataSet. Fill loads the data itself, and FillSchema loads all of the
available metadata about a particular table (such as column names, primary
keys, and constraints). A good way to handle the data loading is to run
FillSchema followed by Fill. If you only use Fill, you can only load the
basic metadata that is required to describe the column names and data
types. The Fill method does not load primary key information. To change
this default behavior, you can set the MissingSchemaAction property of the
DataAdapter object to MissingSchemaAction.AddWithKey, which loads the
primary key metadata along with the default information.

I don't think we need to call
Dim dsA As New ds1
again to fill the data.

For a sample on it, please refer to MSDN article:
"HOW TO: Populate a DataSet Object from a Database by Using Visual C# .NET"
http://support.microsoft.com/?id=314145

I tested it on my side and they are all working fine. There is one thing we
need to pay attention to is that calling FillSchema will result in an extra
trip to the server to retrieve the additional schema information. For best
performance, specify the schema of the DataSet, or set the
MissingSchemaAction of the DataAdapter before calling Fill.

For more information on when to Use FillSchema and MissingSchemaAction with
the DataAdapter in ADO.NET, please refer to MSDN KB article:
"INFO: When to Use FillSchema and MissingSchemaAction with the DataAdapter
in ADO.NET"
http://support.microsoft.com/?id=310128

Does that answer your question?

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dennist

Very useful information. I'll get to it, try it out, and
let you know what happens.

thanks again.

dennist
-----Original Message-----
Hello Dennist,

Thanks for posting in the group.

Based on my experience, the following code should work fine:

daAuthors.FillSchema(dsPubs, SchemaType.Source, "Authors")
daAuthors.Fill(dsPubs, "Authors")

The DataAdapter class provides two methods, Fill and FillSchema, that are
crucial to loading this data. Both of these methods load information into a
DataSet. Fill loads the data itself, and FillSchema loads all of the
available metadata about a particular table (such as column names, primary
keys, and constraints). A good way to handle the data loading is to run
FillSchema followed by Fill. If you only use Fill, you can only load the
basic metadata that is required to describe the column names and data
types. The Fill method does not load primary key information. To change
this default behavior, you can set the
MissingSchemaAction property of the
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top