Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible to create new SQL table from a datatable?

Status
Not open for further replies.

SJG0526

Programmer
Jul 5, 2002
108
0
0
US
I have a datatable built within the program. The data has not come directly from any underlying table. I want to now save this datatable as a new SQL Server table. Is this possible and if so, how?

Thks
 
Yes it is possible, but what do you mean by 'built within the program' ?

 
I have this code (partial shown) and add more columns, then insert rows into the added table. The table only resides in memory, not in SQL Server.
Code:
            Dim workTable As DataTable = mdsTempImport.Tables.Add("TempImport")

            workTable.Columns.Add("UID", Type.GetType("System.Int32"))
            With workTable.Columns.Item("UID")
                .AutoIncrement = True
                .AutoIncrementSeed = 1
                .AutoIncrementStep = 1
            End With
            workTable.PrimaryKey = New DataColumn() {workTable.Columns("UID")}

            workTable.Columns.Add("ImportBatchUID", Type.GetType("System.Int32"))
 
So the host pc of you application will have sql server installed. You want to connect to a database, create a table, and last copy the contents of your built-in to the table in the database.

Do you argee?
 
I don't want to create the SQL table ahead of time. I want the program to create the SQL table just like it was a copy of the structure and data in the TempImport table in the dataset.
 
One way to do this would be to have your code create the table (making sure the table name is not on the server) then loop through all the datatable rows inserting the data one row at a time.
djj
 
Ok then,

Dim a_copy_of_workTable As new datatable
workTable.copy(a_copy_of_workTable)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top