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()
Instead of a
MySqlDataReader
I've used aDataTable
as I find them a little easier to work with but that is preference. I've also implement Using for theMySqlConnection
andMySqlCommand
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
MySqlDbType
s are. You may have to change. I would suggest however savingTimeDate
as just that, aDateTime
.You may also want to implement a little further checking for
DBNulls
onrow(0)
androw(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 theSELECT
statement. So what I am doing is looping through theRows
collection. If you don't want to and you simply want the first row, you can change theSELECT
statement to only bring back the first row which I believe is done usingLIMIT
. This would mean your statement would look something likeSELECT EquipmentID, Quantity FROM subdbborroweq LIMIT 1
. You may want to look at a filter usingWHERE
and you may want to consider ordering your data usingORDER BY
. Alternatively remove theFor Each row
loop and useInteger.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:
Dont you need ' in your string values? VALUES ('John',