Solved

VB.net SQL Append DataTable SQL table

Posted on 2015-01-04
2
321 Views
Last Modified: 2015-01-05
Hi

I create a DataTable that has the same structure as a SQL database table.
What is the best way to add the contents of this DataTable to my SQL table?

Thanks
0
Comment
Question by:murbro
2 Comments
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
Comment Utility
Use DataAdapter's update method to save the datatable to database. Below example code could help you.

Imports System
Imports System.Data
Imports System.Data.SqlClient

Module Module1

    Sub Main()
        Dim sConnectionString As String
        ' Modify the following code to correctly connect to your SQL Server.
        sConnectionString = "Password=StrongPassword;User ID=UserName;" & _
                            "Initial Catalog=pubs;" & _
                            "Data Source=(local)"

        Dim objConn As New SqlConnection(sConnectionString)
        objConn.Open()

        ' Create an instance of a DataAdapter.
        Dim daAuthors As _
            New SqlDataAdapter("Select * From Authors", objConn)

        ' Create an instance of a DataSet, and retrieve data from the Authors table.
        Dim dsPubs As New DataSet("Pubs")
        daAuthors.FillSchema(dsPubs, SchemaType.Source, "Authors")
        daAuthors.Fill(dsPubs, "Authors")

        '*****************
        'BEGIN ADD CODE 
        ' Create a new instance of a DataTable
        Dim tblAuthors As DataTable
        tblAuthors = dsPubs.Tables("Authors")

        Dim drCurrent As DataRow
        ' Obtain a new DataRow object from the DataTable.
        drCurrent = tblAuthors.NewRow()

        ' Set the DataRow field values as necessary.
        drCurrent("au_id") = "993-21-3427"
        drCurrent("au_fname") = "George"
        drCurrent("au_lname") = "Johnson"
        drCurrent("phone") = "800 226-0752"
        drCurrent("address") = "1956 Arlington Pl."
        drCurrent("city") = "Winnipeg"
        drCurrent("state") = "MB"
        drCurrent("contract") = 1

        'Pass that new object into the Add method of the DataTable.Rows collection.
        tblAuthors.Rows.Add(drCurrent)
        MsgBox("Add was successful.")

        'END ADD CODE    
        '*****************
        'BEGIN EDIT CODE

        drCurrent = tblAuthors.Rows.Find("213-46-8915")
        drCurrent.BeginEdit()
        drCurrent("phone") = "342" & drCurrent("phone").ToString.Substring(3)
        drCurrent.EndEdit()
        MsgBox("Record edited successfully")

        'END EDIT CODE   
        '*****************
        'BEGIN SEND CHANGES TO SQL SERVER

        Dim objCommandBuilder As New SqlCommandBuilder(daAuthors)
        daAuthors.Update(dsPubs, "Authors")
        MsgBox("SQL Server updated successfully" & chr(13) & "Check Server explorer to see changes")

        ' END SEND CHANGES TO SQL SERVER 
        '*****************
        'BEGIN DELETE CODE 

        drCurrent = tblAuthors.Rows.Find("993-21-3427")
        drCurrent.Delete()
        MsgBox("Record deleted successfully")

        'END DELETE CODE 
        '*****************
        ' CLEAN UP SQL SERVER
        daAuthors.Update(dsPubs, "Authors")
        MsgBox("SQL Server updated successfully" & Chr(13) & Chr(13) & "Check Server Explorer to see changes")
    End Sub

End Module

Open in new window

0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now