检查DataGridview中检查的行是否存在于数据库

时间:2016-05-26 02:59:18

标签: vb.net datagridview

我有一个DatagridView Checkboxcolumn我的目标是检查数据库中是否已存在Checked Row但我的代码没有提供我想要的输出。怎么了?

这是

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim conn As MySqlConnection = New MySqlConnection("datasource=localhost;database=operations;userid=root;password=admin1950;Convert Zero Datetime=True")
        conn.Open()
        Dim comm As MySqlCommand = New MySqlCommand()
        comm.Connection = conn
        Dim name As String
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1 Step 1
            If Me.DataGridView1.Rows(i).Cells(0).Value = True Then
                name = Me.DataGridView1.Rows(i).Cells(1).Value
                comm.CommandText = "select ElecAssigned,ScheduleDate from assignments where ElecAssigned = '" & name & "' and ScheduleDate = @ScheduleDate"
                comm.Parameters.AddWithValue("@ScheduleDate", DateTimePicker1.Value)
                comm.ExecuteNonQuery()
                Dim reader As MySqlDataReader
                reader = comm.ExecuteReader
                If reader.HasRows Then
                    MsgBox("The persons that you Selected is also Scheduled Today.")
                End If
            End If
        Next
    End Sub

以下是情景。

我在表assignments中保存数据,看起来像这样。

enter image description here

以下是程序中的场景

enter image description here

我在Datagridview中检查了一行并在DateTimePicker中选择了一个与我的表格相同的日期,必须有一条消息,其中会显示The selected Person is also Scheduled Today

这是代码

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connection As New MySqlConnection("datasource=localhost;database=operations;userid=root;password=admin1950;Convert Zero Datetime=True")
        Dim command As New MySqlCommand("SELECT COUNT(*) FROM Assignments WHERE ElecAssigned = @ElecAssigned AND ScheduleDate = @ScheduleDate", connection)
        Dim parameter = command.Parameters.Add("@ElecAssigned", MySqlDbType.VarChar, 50)

        command.Parameters.AddWithValue("@ScheduleDate", DateTimePicker1.Value)

        connection.Open()

        For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If CBool(row.Cells(0).Value) Then
                parameter.Value = CStr(row.Cells(1).Value)

                If CInt(command.ExecuteScalar()) > 0 Then
                    'Match found.

                Else
                    MsgBox("The Personnel(s) you Selected is also Scheduled Today")
                    'No match found.
                    'Save It
                End If
            End If
        Next
    End Sub

无论选择多少,程序都会检查是否也检查了所选人员。

1 个答案:

答案 0 :(得分:1)

E.g。

Dim connection As New MySqlConnection("connection string here")
Dim command As New MySqlCommand("SELECT COUNT(*) FROM Assignments WHERE ElecAssigned = @ElecAssigned AND ScheduledDate = @ScheduledDate", connection)
Dim parameter = command.Parameters.Add("@ElecAssigned", MySqlDbType.VarChar, 50)

command.Parameters.AddWithValue("@ScheduledDate", DateTimePicker1.Value.Date) 'Add a word Date to check only the Date

connection.Open()

For Each row As DataGridViewRow In Me.DataGridView1.Rows
    If CBool(row.Cell(0).Value) Then
        parameter.Value = CStr(row.Cells(1).Value)

        If CInt(command.ExecuteScalar()) > 0 Then
            'Match found.
        Else
            'No match found.
        End If
    End If
Next

您可能需要进行一些小的调整,但如果您想单独了解每个选中的行,那么基本上就是这样做。