我真的不知道如何在标题中提出这个问题,但我会尽力在这里解释它。
首先,我试着看THIS ..问题是,我无法遵循其声明。它在我的脑海里跳舞。
这就是我的目标
获取下一行WHERE fieldFoo = foo
程序流程( MS Access& VB.Net ):
DGV没有连接到数据库,我只是用它们来显示他们将保存/更新的数据。
Dim query As String = "SELECT TOP 1 * FROM piglets WHERE CurrentLocation=@cl AND Week=@week AND SowOrder=@sow ORDER BY ID ASC"
UPDATE
查询。 效果很好但只有一行。 我尝试过:
以下是查找数据的代码
Using cmd As New OleDbCommand(query, con)
With cmd.Parameters
.AddWithValue("@cl", comboFrom.Text)
.AddWithValue("@week", mtWeek.Text)
.AddWithValue("@sow", mtSow.Text)
End With
Dim dr As OleDbDataReader = cmd.ExecuteReader()
If dr.HasRows() Then
If dr.Read() Then
Do
Dim loc As String
loc = dr("CurrentLocation").ToString()
// id = dr("ID")
tempIdList.Add(dr("ID"))
If loc = comboFrom.Text Then
isContinue = True
End If
Loop While dr.Read
End If
Else
isContinue = False
MessageBox.Show("Piglet look up error.", "MR Livestock", MessageBoxButtons.OK, MessageBoxIcon.Error)
clear()
End If
End Using
这是我保存的方式
Dim query As String = "UPDATE Piglets SET Mortality=@mortality, CurrentLocation=@me" & _
" WHERE ID=@id"
// For Each i In tempIdList
For Each row As DataGridViewRow In dgvMortality.Rows
Using cmd As New OleDbCommand(query, con)
With cmd.Parameters
.AddWithValue("@mortality", row.Cells("mortality").Value)
.AddWithValue("@me", "Mortality")
.AddWithValue("@id", tempIdList.Item(id))
End With
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
id = id + 1 // I was hoping here to have the IDs, but I forgot that the query gets only one, so if the are two items, they are the same.
End Using
Next
// Next
我想要的结果是用户能够向DGV添加多个条目,然后可以使用(可能预先保存在列表中)的ID一次保存所有条目来自数据库。现在,我只能保存一个,因为它只获得了TOP 1,即使我输入了我想要的数据。