Copy table schema

  • Thread starter Thread starter JC Voon
  • Start date Start date
J

JC Voon

Hi:

What is the best way to copy the schema of one DataTable to another ?,
The DataTable.Clone will create a new DataTable

Currently i do it manually

destTable.Clear()
destTable.Columns.Clear()
destTable.Constraints.Clear()

For Each dcSrc In srcTable.Columns
dcDest = destTable.Columns.Add()

dcDest.AllowDBNull = dcSrc.AllowDBNull
dcDest.AutoIncrementStep = dcSrc.AutoIncrementStep
dcDest.AutoIncrementSeed = dcSrc.AutoIncrementSeed
dcDest.AutoIncrementStep = dcSrc.AutoIncrementStep
dcDest.Caption = dcSrc.Caption
dcDest.ColumnMapping = dcSrc.ColumnMapping
dcDest.ColumnName = dcSrc.ColumnName
dcDest.DataType = dcSrc.DataType
dcDest.DefaultValue = dcSrc.DefaultValue
dcDest.Expression = dcSrc.Expression
dcDest.MaxLength = dcSrc.MaxLength
dcDest.Namespace = dcSrc.Namespace
dcDest.Prefix = dcSrc.Prefix
dcDest.ReadOnly = dcSrc.ReadOnly
dcDest.Unique = dcSrc.Unique
Next

There must be an easy way to achieve this, any idea ?

JCVoon
 
Hi,

Table.clone is the best way to copy the Schema of One table into another...............

actually ur quest. is not clear can u explin in detail.what exactly u wan a do?
 
Ritesh Jain
Table.clone is the best way to copy the Schema of One table into another...............
actually ur quest. is not clear can u explin in detail.what exactly u wan a do?

Thanks for the info. Here is what i'm trying to do

public sub Caller()
' For some reason i've to create the dt before call MyFunction
dim dt as DataTable = new DataTable
...
MyFunction(dt)
...--------> after the call the dt remain empty (Columns count=0)
end sub


public sub MyFunction(dtDest as DataTable)
dim dtSrc as DataTable
...
dtDest = dtSrc.Clone
...
end sub


In the debuger i can see the dtDest schema, but after return to the
caller the schema gone ! I don't know why
Any idea ?

Thanks
JCVoon
 

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

Back
Top