在访问数据库上选择特定行

时间:2014-03-27 06:34:52

标签: sql .net vb.net ms-access

我真的不知道如何在标题中提出这个问题,但我会尽力在这里解释它。
首先,我试着看THIS ..问题是,我无法遵循其声明。它在我的脑海里跳舞。

这就是我的目标  获取下一行WHERE fieldFoo = foo

的以下ID

程序流程( 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"
  1. 用户插入一个数字,软件检查数据是否存在。如果在那里,它会将它添加到DGV。
  2. 用户需要保存UPDATE查询。 效果很好但只有一行。
  3. 由于它只有一个,它只获得一个ID,这是我基本上用作他们的身份。
  4. 我尝试过:

    1. 我添加了一个整数列表,每次用户查找数据时,它都会包含ID,将其添加到DGV,再次查看,添加到DGV。但它只返回相同的项目,这就是为什么当我尝试保存时,它只更新那一项。
    2. 重新运行sub,但它是一个错误,无法找出原因。
    3. 以下是查找数据的代码

      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,即使我输入了我想要的数据。

0 个答案:

没有答案
相关问题