-1

I'm new to programming and I have a problem with the following code. The 2nd query is not running. It should insert all the data in the first database to the other database.

MySQLConn = New MySqlConnection
MySQLConn.ConnectionString = Connection
Adapter = New MySqlDataAdapter
Dim QRY = "SELECT EquipmentID, Quantity FROM subdbborroweq"
Dim EQID As Integer
Dim QTY As Integer

Dim TimeAndDate As String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")

Try
    MySQLConn.Open() 
    Command = New MySqlCommand(QRY, MySQLConn)
    Reader = Command.ExecuteReader
    While Reader.Read() 
        EQID = Reader(0)
        QTY = Reader(1)
        Dim QRY1 = "INSERT INTO borrowlogs( `BorrowerName`, `EquipmentID`, `Quantity`, `TimeDate`) VALUES (" &
                    AddBorrower.TextBox1.Text & "," & EQID & ", " & QTY & "," &
                    TimeAndDate & ")"

        Command = New MySqlCommand(QRY1, MySQLConn)
    End While

    MySQLConn.Close()
Blackwood
  • 4,504
  • 16
  • 32
  • 41
Newbie
  • 39
  • 4
  • 1
    From what I can see you're not executing the command. – Bugs Feb 02 '17 at 19:11
  • " Command = New MySqlCommand(QRY1, MySQLConn)" Sorry kind of messy post. im new in here – Newbie Feb 02 '17 at 19:12
  • Execute that command. `Command.ExecuteNonQuery` – Bugs Feb 02 '17 at 19:13
  • You should also look at using parameters. You're open to SQL injection. – Bugs Feb 02 '17 at 19:15
  • 1
    Thank you bro. sorry for my stupid question. HAHAHAH i'm really new in this profession. i hope you can teach me or i can ask you a question if ever i have problem again. – Newbie Feb 02 '17 at 19:15
  • No problem. Seriously though, look into using parameters. – Bugs Feb 02 '17 at 19:18
  • Bro last question, i have this sql syntax error you have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax ner '4,3'2017/02/03 03:21:13')' at line 1 – Newbie Feb 02 '17 at 19:22
  • Bro what parameters, i don't know that. can you show me a demonstration for that? i mean can you demo it on this code. – Newbie Feb 02 '17 at 19:24
  • Okay ill send you a link of my codes then you look at it. it's really confusing for now. Sorry to bother you alot. I hope you'll still help me http://paste2.org/bwEIfv7p – Newbie Feb 02 '17 at 19:37
  • i cant put the execute inside the loop cuz it has conflict with DataReader – Newbie Feb 02 '17 at 19:55

2 Answers2

1

I have put together some code but please note this is untested as I don't use MySQL anymore.

Instead of a MySqlDataReader I've used a DataTable as I find them a little easier to work with but that is preference. I've also implement Using for the MySqlConnection and MySqlCommand objects. This is so the objects are disposed of properly and you don't have to worry about do that.

Please note that I don't know your data structure. I have taken a guess of what the MySqlDbTypes are. You may have to change. I would suggest however saving TimeDate as just that, a DateTime.

You may also want to implement a little further checking for DBNulls on row(0) and row(1). I've left this to you to look at, it may not be necessary but it's always worth looking into as they do cause problems when the crop up.

I'm unsure how you want to handle multiple rows in your DataTable brought back from the SELECT statement. So what I am doing is looping through the Rows collection. If you don't want to and you simply want the first row, you can change the SELECT statement to only bring back the first row which I believe is done using LIMIT. This would mean your statement would look something like SELECT EquipmentID, Quantity FROM subdbborroweq LIMIT 1. You may want to look at a filter using WHERE and you may want to consider ordering your data using ORDER BY. Alternatively remove the For Each row loop and use Integer.TryParse(dt.Rows(0).Item(0).ToString(), EQID)

This is the code I have put together. It may not be 100% but hopefully it will give you something to go on:

Dim dt As New DataTable

Using con As New MySqlConnection(Connection),
      cmd As New MySQLCommand("SELECT EquipmentID, Quantity FROM subdbborroweq", con)

    con.open()

    dt.Load(cmd.ExecuteReader)

    If dt.Rows.Count > 0 Then

        cmd.CommandText = "INSERT INTO borrowlogs(BorrowerName, EquipmentID, Quantity, TimeDate) VALUES (@Uname, @EQID, @QTY, @TAD)"

        cmd.Parameters.Add("@Uname", MySqlDbType.VarChar)
        cmd.Parameters.Add("@EQID", MySqlDbType.Int32)
        cmd.Parameters.Add("@QTY", MySqlDbType.Int32)
        cmd.Parameters.Add("@TAD", MySqlDbType.DateTime)

        For Each row As DataRow In dt.Rows

            cmd.Parameters("@Uname").Value = AddBorrower.TextBox1.Text
            cmd.Parameters("@EQID").Value = row.Field(Of Int32)(0)
            cmd.Parameters("@QTY").Value = row.Field(Of Int32)(1)
            cmd.Parameters("@TAD").Value = DateTime.Now

            cmd.ExecuteNonQuery()

        Next

    End If

 End Using
Bugs
  • 4,491
  • 9
  • 32
  • 41
  • @Plutonix, thanks for the edit. I didn't know that was something I could do. – Bugs Feb 03 '17 at 15:48
  • The site applies the highlighting of the most popular tag on the question, in this case MySql, the `...lang-vb..` tag overrides that. That code could create one connection and one command object (outside the loop) then just change the parameter values inside it – Ňɏssa Pøngjǣrdenlarp Feb 03 '17 at 16:05
  • @Plutonix thanks re the tag, learn something everyday. Re your second statement, that was something I did consider but wasn't sure which method was correct and went with this as it seems simpler to follow. Are you saying I would wrap the whole code in the first `Using` loop and then update the `cmd.CommandText` for the `Insert` when it came to it and then just set the parameter values on each loop? – Bugs Feb 03 '17 at 16:09
  • [Here is an example](http://stackoverflow.com/a/34809111/1070452) though the outer Connection and Command Using blocks arent there. See also [Can we stop using AddWithValue() already?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) – Ňɏssa Pøngjǣrdenlarp Feb 03 '17 at 16:14
  • @Plutonix I read something the other day that said `Add` was depreciated and to use `AddWithValue()`. I even updated one of my other answers until I re-read the `MySQL` source again and realised that `public MySqlParameter Add(string parameterName, MySqlDbType dbType)` is not depreciated however `public MySqlParameter Add(string parameterName, object value)`. Hence the use of `AddWithValue`. I'll be updating my answer. Thanks for the pointers. – Bugs Feb 03 '17 at 16:16
  • 1
    There is one overload of `Add` that is deprecated - the `(string, Object)` form because it results in the same guessing game as AddWithValue. (its the same for all the providers: OleDB, MySQL, SQLite, ODBC, SQL Server etc) – Ňɏssa Pøngjǣrdenlarp Feb 03 '17 at 16:26
  • 1
    @Plutonix, yeah that's what I found when I re-read the source. I've updated the answer. I think it suits what you suggested. Only thing I can't confirm is the `MySqlDbType`s. – Bugs Feb 03 '17 at 16:34
  • 1
    Yeah, I think the OP was using all strings. Get rid of the empty try/Catch-es and I will upvote – Ňɏssa Pøngjǣrdenlarp Feb 03 '17 at 16:39
  • 1
    @Plutonix done, thanks for the suggestions and upvote. Appreciate it. – Bugs Feb 03 '17 at 16:45
  • 1
    FYI, you can have linq to cast the DR values for you: `Foo = dr.Field(Of Int32)(0) ... Bar = dr.Field(Of String)(1)`; What you have is gobs better than what the OP was trying, but `Field(Of)` would be cleaner than ToString-TryParse – Ňɏssa Pøngjǣrdenlarp Feb 03 '17 at 16:53
0

Dont you need ' in your string values? VALUES ('John',

$QRY1 = "INSERT INTO borrowlogs( BorrowerName, EquipmentID, Quantity, TimeDate) VALUES ('" & AddBorrower.TextBox1.Text & "','" & EQID...
Jim VanPetten
  • 413
  • 3
  • 11