Dataset based on Sql Server DB

A

Ale K.

How can i create an empty dataset based on a SQL Server DB, with no need
to connect every time to the DB to get the structure.... something like get
a XSD from the DB and use that structure.

Somebody can help me with this??

Thanks.
Alex.
 
W

William Ryan

I'm not exactly sure what you need, but let me take a shot
it.

If you run a query for instance, SELECT * FROM someTable
and use a SQLDataReader for instance, you can choose you
could use something like DataTable dt =
myDataReader.GetSchemaTable();

This will give you all of the information about the schema
that was used in the query. 'Connect every time' though
is what I'm wondering about. Since you are disconnected,
if the structure changes, you won't know about it without
either re-querying the DB or using some fancy footwork
with SQL Server Notification.

Anyway, once you have your datatable, add it to the
dataset and you have that
information....myDataset.Tables.Add(dt);

If you need all of the information in all of the tables of
the db, you could query SysObjects like SELECT NAME FROM
Sysobjects wherek XType = 'U'...you can use a DataReader
ro DataTable, but it'd probably make more sense to use a
Reader since you don't update this table. Then, you could
iterate through it and add the schema information to the
dataSet like this.
Dim cmd as New SqlCOmmand

While dr.Read()
sql = SELECT * FROM & dr(0)
Dim dta as New DataTable
If cn.State <> ConnectionState.Open then cn.Open
cmd.CommandText = sql
dr = cmd.ExecuteReader(CommandBehavior.SchemaOnly)
Dim dt as new DataTable
dt = dr.GetSchemaTable
myDataSet.Tables.Add(dt)

dr.Close
cn.Close

End While

Note that this is pseudo-code for how to attempt it but
it's pretty close to what would work. By using the
SchemaOnly, you'd accomplish your goal of getting an empty
datatable with the column info.

Good Luck,

Bill
 

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