I have a winforms application connected to Mysql using the .Net driver. I written a class to handle to all the database functionality. I use a dataTable to store query results for binding from mysql.
Imports MySql.Data.MySqlClient
Imports System.IO
Public Class MysqlConSjC
Private conn As MySqlConnection
Private connStr As String
Public strace As String
Private status As Integer
'status member variable for connectivity. 0 for success,
'value of one indicates db connection error
'value of 2 indicates query failure
Private comd As MySqlCommand
Private adpter As MySqlDataAdapter
Private dta As DataTable
Public Sub Query(ByVal SQl As String)
Try
comd.Connection = conn
comd.CommandText = SQl
adpter.SelectCommand = comd
adpter.Fill(dta)
status = 0
Catch ex As MySqlException
strace = ex.StackTrace()
DisconnectMysql()
status = 2
End Try
End Sub
This method does a select query. How would I write a method to insert/update/delete rows of the DataTable in the database?
If the provider was able to create the update statements (UPDATE, INSERT, DELETE) for the given command, then I believe you can simply call the
Updatemethod on the data adapter after making updates to the DataTable.The DataTable object tracks changes made to itself. The data adapter object then uses that information to determine which SQL statements (UPDATE, INSERT, DELETE) to run.