-1

I've searched around the web looking for samples on how to use MySqlHelper.UpdateDataSet but all I've found is:

Public Shared Sub UpdateDataSet( _
   ByVal connectionString As String, _
   ByVal commandText As String, _
   ByVal ds As DataSet, _
   ByVal tablename As String _
) 

I'll be grateful if someone will give me:

  • an example of commandText because I didn't understand what kind of command I need to give;
  • an explanation of tablename because I need to know if is the name of a table of the DB or of the DataSet or both (with the same name);
  • a vb.net code example (to start testing).

I tryed to use the command this way:

Private Sub Btn_Mod_Dataset_Click(sender As Object, e As EventArgs) Handles Btn_Mod_Dataset.Click
    Dim SqlStr$ = "SELECT * FROM MyTest.Users"
    Using DS_Test As DataSet = DB_Functions.mQuery(SqlStr)
        With DS_Test
            .Tables(0).TableName = "Users"
            Dim User$ = .Tables(0).Rows(0)("UserName").ToString
            MsgBox(User)
            .Tables(0).Rows(0)("User") = "Upd_Test"
            User = .Tables(0).Rows(0)("UserName").ToString
            MsgBox(User)
        End With
        Dim DB_Name = "MyTest"
        Dim connectionString$ = "datasource=localhost;username=" + UserDB _ 
            + ";password=" + Password + ";database=" + DB_Name
        MySqlHelper.UpdateDataSet(connectionString, _
            "Update MyTest.Users Set UserName = 'Test_Ok' WHERE UserName = 'Steve'", _
            DS_Test, "Users")
    End Using
End Sub

This gives me

System.NullReferenceException' in System.Data.dll

EDIT (to explain my code):
a) DB_Functions is a sepate class where I've stored some function to use on a MySql DataBase. mQuery is a function who extract query result into a dataset;
b) 'User' is a field Name : I've changed it to 'UserName' but same result;

d) The code between With and End With is just a test to see what happens;

NOTE that the code gives error but my DB is updated as in the commandText String. I don't understand what happens

genespos
  • 3,211
  • 6
  • 38
  • 70
  • As [the docs](https://dev.mysql.com/doc/connector-net/en/connector-net-ref-mysqlclient-mysqlhelpermembers.html) explain, commandtext is the SQL to execute, tableName would be the name of the table in the DS to act on. Frankly the db provider objects are not so difficult to use that the Helper seems necessary - a [fully configured DataAdapter](http://stackoverflow.com/a/33702351) is pretty easy to use and there can be almost no SQL in your app. – Ňɏssa Pøngjǣrdenlarp Apr 26 '16 at 15:44
  • @Plutonix Thanks for your comment but It seems I'm not able to use this command. Can you please say me what is wrong in my test? (anyway I'm looking at the dataadapter code) ;) – genespos Apr 26 '16 at 16:08
  • Could be several things. a) no idea what `DB_Functions` is or does, but it looks like another helper at which point you are close to creating an inner platform. b) `User` is a reserved word in MySQL, c) that whole thing would be simpler with parameters, d) `.Tables(0).Rows(0)("User")` usage is odd - you get the value, then set it to `"Upd_Test"` then get the value again (??). e) the DS is a local variable. – Ňɏssa Pøngjǣrdenlarp Apr 26 '16 at 16:20
  • @Plutonix I add an edit. On your 'e)' I don't have any 'DS' variable in my code what do you mean? – genespos Apr 26 '16 at 17:12
  • `DS_Test As DataSet` is a DataSet (DS). Generally, if you bother to create one, you want them to linger longer than the life of one procedure. – Ňɏssa Pøngjǣrdenlarp Apr 26 '16 at 17:25

2 Answers2

1

This might get you part of the way.

First get rid of DB_Functions. MySQLHelper has a method to create the DataSet for you; in general, db Ops are so query-specific that there is very little that is generic and reusable. The exception to this is building the ConnectionString: MySQL has gobs of cool options you can enable/disable via the connection string. But for that you just need the standard MySqlConnectionStringBuilder.

Build a DataSet:

' form/class level vars
Private dsSample As DataSet
Private MySqlConnStr As String = "..."

...
Dim SQL = "SELECT Id, FirstName, Middle, LastName FROM Employee"

Using dbcon As New MySqlConnection(MySQLConnStr)
    dsSample = MySqlHelper.ExecuteDataset(dbcon, SQL)
    dsSample.Tables(0).TableName = "Emps"
End Using

There does not appear to be a way to specify a tablename when you build it, so that is a separate step.

Update a Record

To update a single row, you want ExecuteNonQuery; this will also allow you to use Parameters:

Dim uSQL = "UPDATE Employee SET Middle = @p1 WHERE Id = @p2"
Using dbcon As New MySqlConnection(MySQLConnStr)
    Dim params(1) As MySqlParameter

    params(0) = New MySqlParameter("@p1", MySqlDbType.String)
    params(0).Value = "Q"

    params(1) = New MySqlParameter("@p2", MySqlDbType.Int32)
    params(1).Value = 4583

    dbcon.Open()
    Dim rows = MySqlHelper.ExecuteNonQuery(dbcon, uSQL, params)
End Using

Again, this is not really any simpler than using a fully configured DataAdapter, which would be simply:

dsSample.Tables("Emps").Rows(1).Item("Middle") = "X"
daSample.Update(dsSample.Tables("Emps"))

I am not exactly sure what value the UpdateDataSet method adds. I think it is the "helper" counterpart for the above, but since it doesn't provide for Parameters, I don't have much use for it. The docs for it are sketchy.

The commandtext would appear to be the SQL for a single row. Note that the DataAdapter.Update method above would add any new rows added, delete the deleted ones and update values for any row with changed values - potentially dozens or even hundreds of db Ops with one line of code.

Community
  • 1
  • 1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Thanks for your answer, but I'm already able to update DB from DataSet/DataTable (see my code here: http://stackoverflow.com/questions/34441251/function-for-inserting-updating-mysql-db-returns-wrong-affected-rows-number). I was just asking how to use MySqlHelper.UpdateDataset. Anyway +1 for your consideration – genespos Apr 27 '16 at 07:24
  • The point was that using a DataAdapter, it is very simple to update a db with the data in a DataTable. Just one line of code: `daSample.Update(dsSample.Tables("Emps"))` . Code such as you linked to just isnt needed. See the [first link I gave you](http://stackoverflow.com/a/33702351) for how to set them up. – Ňɏssa Pøngjǣrdenlarp Apr 27 '16 at 17:09
0

My funtion gets string query and returns datatable. So u can set dataset.tables .

Public Function mysql(ByVal str_query As String) As DataTable
   Dim adptr As New MySqlDataAdapter
   Dim filltab As New DataTable
   Try
       Using cnn As New MySqlConnection("server=" & mysql_server & _
           ";user=" & mysql_user & ";password=" & mysql_password & _
           ";database=" & mysql_database & ";Allow User Variables=True")
           Using cmd As New MySqlCommand(str_query, cnn)
               cnn.Open()
               adptr = New MySqlDataAdapter(cmd)
               adptr.Fill(filltab)
               cnn.Close()
           End Using
       End Using
   Catch ex As Exception
           'you can log mysql errors into a file here log(ex.ToString)
   End Try
   Return filltab
End Function
genespos
  • 3,211
  • 6
  • 38
  • 70