Solved

Save DataTable changes back to DB through wrapper class in library

Posted on 2015-02-09
1
31 Views
Last Modified: 2016-01-11
I have an application that can use two different Databases, so I wrote a wrapper class for each Database.

Here is a snippet of the code from the wrapper library:
    Public Function ExecuteQuery(SQL As String) As DataTable
        Return GetDataTable(SQL, Nothing)
    End Function
    Private Function GetDataTable(ByVal query As String, ByVal ParamArray args() As MySqlParameter) As DataTable
        Dim dt As New DataTable
        Dim cmd As New MySqlCommand(query, GetConnection())
        If args IsNot Nothing Then
            For Each p As MySqlParameter In args
                cmd.Parameters.Add(p)
            Next p
        End If

        Dim da As New MySqlDataAdapter(cmd)
        da.Fill(dt)
        Return dt

    End Function

Open in new window


I can call this code from my main program and it works just fine.  The problem I have to writing back to the database.  For simple single commands I can handle this easily, but I have one situation where the user will be making multiple Insert/Edit/Delete operations on a large DataTable.  How can I simply dump these changes back into the database in one step?

I've never used DataAdapters and CommandBuilders but they look like what I need.  But how can I expose those from the library without the main application requiring a reference to the specific database dll?  (I cannot put a reference to the actual DB dll in my main code because of a version conflict.  Using a wrapper class was my solution to this problem).
0
Comment
Question by:MasterWoodsman
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 40603410
If your tables have primary keys, then you can use the MySqlCommandBuilder with the MySqlDataAdapter to auto-generate the CRUD commands for the data adapter.

Please describe the "version conflict" comment that you made.  Do you need two different versions of the same database assembly?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

20 Experts available now in Live!

Get 1:1 Help Now