Results 1 to 9 of 9

Thread: Insert a datatable into d database

  1. #1

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200

    Insert a datatable into d database

    O.K. i have extracted info from one database and have placed the info in a datatable. I want to take that datatable and place the info in it in a different database that does not contain that table. i wnat to create a new table in the database and have it equal the datatable i have extracted.

    How can i do this? Is it doable? If so how....

    Thanks!!!

  2. #2
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    I haven't done it myself, but Balena's text demonstrates it in some detail. Here's enough to get you started, I think.

    Sub Create EmployeesTable()
    Dim dtEmp as New DataTable("Employees")
    dtEmp.MinimumCapacity = 100
    dtEmp.CaseSensetive = False, etc etc

    ' create columns
    Dim dcFName as New DataColumn("FirstName", GetType(String))
    dtEmp.Columns.Add(dcFname)
    ' another way
    dtEmp.Columns.Add("City", GetType(String)).MaxLength = 20
    ' Create an ID
    Dim dcEmpId as New DataColumn("EmpID", GetType(Integer))
    dcEmpID.AutoIncrement = True
    dcEmpID.AutoIncrementSeed = 1
    dcEmpID.AllowDBNull = False, etc, etc
    dtEmp.Columns.Add(dcEmp)
    ' Make it a primary key
    Dim pkCols() as DataColumn = {deEmpId}
    dtEmp.PrimaryKey = pkCols

    ' add the table to a dataset
    ds.Tables.Add(dtEmp)

    there you go

  3. #3

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    THat is not exactly what i meant, I know how to do that but i do not know how to put the datatable that i have created into an existing database.

    for example i created a table with 3 columns and i want to store the newly created table in a database say...mydb.mdb and i want to save the datatable as a new table in the mydb.mdb database how could i do that?

    Thanks for your help

  4. #4

  5. #5

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    would this be possible?

    Use sql to create a table witht the exact name of the datatable i have. Name the colums in the database table the same as the columns i have in the datatable. Then just use the .update command on a dataset object.

    Would this work if it were done right?

  6. #6

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    bump

  7. #7
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    Originally posted by Buy2easy.com
    would this be possible?

    Use sql to create a table witht the exact name of the datatable i have. Name the colums in the database table the same as the columns i have in the datatable. Then just use the .update command on a dataset object.

    Would this work if it were done right?
    I assume so, providing you have the proper InsertCommand. Why not try and see?

  8. #8

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    hello, i am in the process of adding the table and i have created a table in the new database with the columns the same name as the ones i want to insert but i am having trouble with one thing i am making the columns a text type in the database but columns are not a text type in the datatable. i tried to change each of the columns to a datatype string like

    whateverthecolumniscalled.datatype = system.type.gettype("system.string")

    but that is saying that a string type can not be found ar something.

    how can i change the properties of the datatable so that they are of a string type or a text type?

    thanks!

  9. #9
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    I posted some code in the C# forum that shows how to do this entire process, including setting the data types of the columns in both the dataset and the table. It's pretty close to VB. Note the code I am referring to is near the bottom of the thread.

    http://www.vbforums.com/showthread.p...hreadid=251282

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width