MySQLConn = New MySqlConnection
MySQLConn.ConnectionString = Connection
Adapter = New MySqlDataAdapter
Dim QRY = "SELECT EquipmentID, Quantity FROM subdbborroweq"
Dim TimeAndDate As String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
Dim EQIDArr As New ArrayList
Dim QTYArr As New ArrayList
Try
MySQLConn.Open()
Command = New MySqlCommand(QRY, MySQLConn)
Dim QRY1 = "INSERT INTO borrowlogs( `BorrowerName`, `EquipmentID`, `Quantity`, `TimeDate`) VALUES (@Uname, @EQID, @QTY, @TAD )"
Dim Command2 = New MySqlCommand(QRY1, MySQLConn)
Reader = Command.ExecuteReader
While Reader.Read()
EQIDArr.Add(Reader("EquipmentID"))
QTYArr.Add(Reader("Quantity"))
End While
Reader.Close()
Dim i As Integer = 0
For Each eqid As Integer In EQIDArr
For Each qty As Integer In QTYArr
Command2 = New MySqlCommand(QRY1, MySQLConn)
Command2.Parameters.AddWithValue("@Uname", AddBorrower.TextBox1.Text)
Command2.Parameters.AddWithValue("@EQID", eqid)
Command2.Parameters.AddWithValue("@QTY", qty)
Command2.Parameters.AddWithValue("@TAD", TimeAndDate)
Command2.ExecuteNonQuery()
Next
Next
MySQLConn.Close()
AddBorrowerIntoBorrowerForm.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySQLConn.Dispose()
End Try
AddBorrower.Close()
我有这个代码,据说将一个表集转移到另一个表但添加一些细节并加强它,我有问题
For Each eqid As Integer In EQIDArr
For Each qty As Integer In QTYArr
Command2 = New MySqlCommand(QRY1, MySQLConn)
Command2.Parameters.AddWithValue("@Uname", AddBorrower.TextBox1.Text)
Command2.Parameters.AddWithValue("@EQID", eqid)
Command2.Parameters.AddWithValue("@QTY", qty)
Command2.Parameters.AddWithValue("@TAD", TimeAndDate)
Command2.ExecuteNonQuery()
Next
Next
部分。任何人都可以帮我这个循环吗?我的问题是,当我按下按钮时,它将表格输入到它的列数。请有人帮这个循环吗?
答案 0 :(得分:0)
为什么要为此使用循环?您只需使用insert . . . select
即可。按照你要做的逻辑来说有点难,但这可能会让你走上正轨:
INSERT INTO borrowlogs( `BorrowerName`, `EquipmentID`, `Quantity`, `TimeDate`)
SELECT AddBorrower.TextBox1.Text, EquipmentID, Quantity, NOW()
FROM subdbborroweq;