Total Pageviews

Monday, September 22, 2014

ASP.net insert datatable into the database using dataadapter

ASP.net insert datatable into the database using dataadapter : To do this your datatable schema to be inserted should match with table schema of database. It will work on all database connection string:

Assuming said above your datatable is dtc then call insertTableinDB(dtc, "teamMaster")


     

         Public Shared Function insertTableinDB(ByVal dt As DataTable, ByVal mytablename As String) As String

        'Create Connection String
        Dim myquery = "select * from " & mytablename & " limit 1"
        Using connection As New SQLiteConnection(System.Configuration.ConfigurationManager.ConnectionStrings("vindb").ConnectionString)

            Try
                'connection.Close()
                connection.Open()
                '' sqlComm = New SQLiteCommand("select * from gamemaster", connection)
                Dim da As New SQLiteDataAdapter(myquery, connection)
                Dim dcmd As SQLiteCommandBuilder = New SQLiteCommandBuilder(da)
                da.MissingSchemaAction = MissingSchemaAction.AddWithKey
                da.Update(dt)
                ''  sqlComm.Dispose()
                da.Dispose()
                connection.Close()
                insertTableinDB = "ok"


            Catch exp As Exception
                'lbldebug.Text = exp.Message
                connection.Close()
                insertTableinDB = "Error:" + exp.Message

            End Try
            '' Close Database connection
            '' and Dispose Database objects
        End Using

    End Function

No comments: