none
删除两个表的数据 RRS feed

  • 问题

  • cmd.CommandText = "select * from staff"

            cmd.Connection = MyConnection

     

            For i = Me.deletedtbase.SelectedRows.Count - 1 To 0 Step -1

     

                cmd.CommandText = "delete from staff where Staff_ID=" & deletedtbase.SelectedRows(i).Cells("Staff_ID").Value

          cmd.CommandText = "delete from login where Staff_ID=" & deletedtbase.SelectedRows(i).Cells("Staff_ID").Value

               

                MyConnection.Open()

                cmd.ExecuteNonQuery()

                MyConnection.Close()

                Me.deletedtbase.Rows.Remove(Me.deletedtbase.SelectedRows(i))

     

            Next

    我想要在button1直接删除两个表的数据,该怎样写?上面的代码只能删除一个表的数据。

    2011年8月28日 7:17

答案

  • 你好

    或者你可以嘗試

    把cmd.CommandText 放在 MyConnection.Open() 之後

    之後在第一個 cmd.CommandText = "delete from staff where Staff_ID=" & deletedtbase.SelectedRows(i).Cells("Staff_ID").Value"

    加上  cmd.ExecuteNonQuery()

    E.G.

     For i = Me.deletedtbase.SelectedRows.Count - 1 To 0 Step -1
          MyConnection.Open() 
          cmd.CommandText = "delete from staff where Staff_ID=" & deletedtbase.SelectedRows(i).Cells("Staff_ID").Value
          cmd.ExecuteNonQuery()
     
      cmd.CommandText = "delete from login where Staff_ID=" & deletedtbase.SelectedRows(i).Cells("Staff_ID").Value
          
    
          cmd.ExecuteNonQuery()
          MyConnection.Close()
          Me.deletedtbase.Rows.Remove(Me.deletedtbase.SelectedRows(i))
     
        Next
    


    Please correct me if my concept is wrong

     


    Chi
    • 已标记为答案 ckjason 2011年8月28日 15:17
    2011年8月28日 8:36
    版主