Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 24th, 2004, 05:47 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding a New Table to a Database with ADO.NET

I have a DataTable which I've created from scratch as part of a dataset in a VB application. How can I write it back to the data source, in this case an Access file?

Thanks!

Aaron

 
Old December 25th, 2004, 11:19 AM
Authorized User
 
Join Date: May 2004
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

Dear Aaron!

You can save the DataTable back 2 your SQL Server database in 2 ways:
1 - You can loop through each row in the DataTable, build a SQL statement dynamically and excute it against the database, OR
2- You can use a SqlDataAdapter object, configure its InsertCommand and call its update method passing it the DataTable object.

Assume the following:-
1 - You have an open conn named 'nwind' to the Northwind SQL Server db
2 - Your DataTable's name is custsTable and is populated with data
3 - The custsTable represents Customers table & contains these fields
    (a) CustomerID
    (b) CompanyName
    (c) ContactName
4 - You have a DataSet named nwindDataSet and you have added your table, custsTable, to this DataSet

Now here goes method 1:-

------------------------------------------------
Dim custsInsCmd As SqlClient.SqlCommand
Dim custRow As DataRow
Dim sql As String

custsInsCmd = nwind.CreateCommand()
For Each custRow In custsTable.Rows
    sql = "INSERT INTO Customers (CustomerID, " & _
        "CompanyName, ContactName) VALUES (" & _
        "'" & custRow("CustomerID") & "', " & _
        "'" & custRow("CompanyName") & "', " & _
        "'" & custRow("ContactName") & "')"
        custsInsCmd.CommandText = sql
    custsInsCmd.ExecuteNonQuery()
Next
------------------------------------------------

Now here goes method 2:-

------------------------------------------------
Dim custsAdapter As New SqlClient.SqlDataAdapter
Dim custsInsCmd As SqlClient.SqlCommand()

' Create Insert cmd and set its Command Text
custsInsCmd = nwind.CreateCommand()
custsInsCmd.CommandText = "INSERT INTO Customers (CustomerID, " & _
  "CompanyName, ContactName) VALUES (@CustomerID, @CompanyName, " & _
  "@ContactName)"
custsInsCmd.CommandType = CommandType.Text

' Add parameters to the Insert command
custsInsCmd.Parameters.Add("@CustomerID", SqlDbType.NChar, _
    5, "CustomerID")
custsInsCmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, _
    40, "CompanyName")
custsInsCmd.Parameters.Add("@ContactName", SqlDbType.NVarChar, _
    30, "ContactName")

' Initialize the DataAdapter's Insert Command
custsAdapter.InsertCommand = custsInsCmd

' Send data table rows back to the data source
custsAdapter.Update(custsTable)
------------------------------------------------

I hope this code helps you.



ejan
 
Old December 25th, 2004, 11:31 AM
Authorized User
 
Join Date: May 2004
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

Dear Aaron!

Method 1 will work 4 Access databases. I'm not sure about whether Method 2 will work 4 Access databases or not.

4 SQL server both Method 1 and 2 will work.

Ragards,


ejan
 
Old December 26th, 2004, 01:45 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Ejan,

Both your solutions were excellent, but they also assume that there is already a table in the database that I want to Insert into, in the case of your example, the Customers table in Northwind.

But this is a brand new table I want to insert, or add to the database. I suppose I could just pass a Create Table SQL command using the OleDbCommand object, and then populate it using one of the methods you recommended. But I was hoping ADO.NET had a simple method to Add a DataTable from a Dataset to a database.

Cheers and Happy Holidays

Aaron

 
Old December 26th, 2004, 03:04 AM
Authorized User
 
Join Date: May 2004
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well Aaron,

As long as my information is concerned, there is no object in the ADO.Net library to create a table the way you desire. If you must do it that way, checkout the ADOX library (cousin of the ADODB library:))

But wait man! have you not asked that you want a SIMPLE method for adding a table to a database? If so, then I assure you that the most efficient and easiest method of creating a table in the database from an application is to just pass a simple CREATE TABLE statement. I recommend using the CREATE TABLE statement rather than ADOX library or any other library around.

Regards,

ejan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a column to an existing SQL database table furjaw SQL Language 10 May 17th, 2007 06:35 PM
Adding New Record to Access Database Using VB.NET TechHelp ASP.NET 1.0 and 1.1 Basics 1 September 13th, 2006 03:19 AM
Create table from another table using ADO Kaustav Pro VB Databases 0 October 15th, 2005 05:44 AM
Adding new field to a database table in run time dotnet cat ASP.NET 1.0 and 1.1 Basics 1 October 6th, 2005 01:36 PM
Adding rows to a table in ASP.NET codebehind GilletteCat ASP.NET 1.0 and 1.1 Professional 2 August 3rd, 2004 02:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.