我的方法,VALIDATE和INSERT代码都有一个谜

时间:2013-02-21 15:51:07

标签: vb.net if-statement sqlcommand

这是我的INSERT代码:

Dim _cmd as new SQLCommand()

Private Sub _btAdd_ANA_GRUPO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _btAdd_ANA_GRUPO.Click

    _resultCMD = 0

    'Por cada linha da grid analises
    For Ro = 0 To DataGrid_AnaEXT.Rows.Count - 1

        If DataGrid_AnaEXT.Rows(Ro).Cells("Selected").Value = 1 Then

            _cmd.Parameters.Clear() ' here i clear because i use the _cmd other sections

           '**enigma**'
           _query = "INSERT into dbo.tbl_ENC_GRUPO_ANA_LINHA VALUES( @idG, @idAna, @sigla, @nomeA, @_ck )"  / If i put the query string here ,the INSERT does nothing

            'Dim _cmd As New SqlCommand - I tried to declare here but same problem

            _cmd.Parameters.Add("@idG", SqlDbType.Int).Value = _dgvGruposA.CurrentRow.Cells("ID_GRUPO_ANA").Value
            _cmd.Parameters.Add("@idAna", SqlDbType.Char).Value = Trim(DataGrid_AnaEXT.Rows(Ro).Cells("ANA_ID").Value.ToString())
            _cmd.Parameters.Add("@sigla", SqlDbType.VarChar).Value = Trim(DataGrid_AnaEXT.Rows(Ro).Cells("ANA_SIGLA").Value.ToString())
            _cmd.Parameters.Add("@nomeA", SqlDbType.VarChar).Value = _selectAnaNome(Trim(DataGrid_AnaEXT.Rows(Ro).Cells("ANA_ID").Value.ToString()))
            _cmd.Parameters.Add("@_ck", SqlDbType.Bit).Value = DataGrid_AnaEXT.Rows(Ro).Cells("Selected").Value

            'Here i check if and ID exist in destination table "_checkAnaNoGrupo" , to not duplicate


            If _checkAnaNoGrupo(_dgvGruposA.CurrentRow.Cells("ID_GRUPO_ANA").Value, (DataGrid_AnaEXT.Rows(Ro).Cells("ANA_ID").Value.ToString)) = False Then

                _query = "INSERT into dbo.tbl_ENC_GRUPO_ANA_LINHA VALUES( @idG, @idAna, @sigla, @nomeA, @_ck )"


                _cmd.Connection = _con.connect
                _cmd.CommandText = _query
                _cmd.CommandType = CommandType.Text

                Try
                    _con.connect.Open()
                    _resultCMD = _resultCMD + _cmd.ExecuteNonQuery()
                    _con.connect.Close()

                Catch ex As Exception
                    _con.connect.Close()
                    MessageBox.Show("Erro Linha : " & ex.Message & " !")

                End Try

            End If

        End If
    Next

    If _resultCMD > 0 Then
        MessageBox.Show("Adicionou : " & _resultCMD _
                        & " análises ao Grupo : " & _dgvGruposA.CurrentRow.Cells("NOME_G").Value & " !")
    End If

    _selectAnaLinhaG(CInt(_dgvGruposA.CurrentRow.Cells(0).Value))

End sub 

这是验证是否存在的函数 - 它工作正常

Public Function _checkAnaNoGrupo(ByVal _grupoA As Integer, ByVal _codAna As String) As Boolean

    _tabCheckAnaNoGrupo() 

    Dim _ck As Boolean = False

    For Each _tRow As DataRow In _tabCheckAnaNoGrupo.Rows
        If _tRow("FK_ID_GRUPO_ANA").ToString.Contains(_grupoA) And _tRow("ID_ANA").ToString.Contains(_codAna) Then
            _ck = True
        Else
            _ck = False
        End If
    Next

    Return _ck

End Function

这里我将SQL表中的数据导入DataTable:

 Public Function _tabCheckAnaNoGrupo() As DataTable

    Dim _tabCKana As New DataTable

    _query = "SELECT * FROM dbo.tbl_ENC_GRUPO_ANA_LINHA"

    _adapt.SelectCommand = New SqlCommand(_query, _con.connect)

    _adapt.Fill(_tabCKana)

    Return _tabCKana

End Function

我的问题是: 如果我运行像这样的代码工作正好INSERT语句,但重复行。

但是如果我运行带有_query放置在' enigma '中的代码(参见UP),则INSERT不会执行任何操作。没有错误 - 没有错误

我希望你可以帮助我,可能是一个愚蠢的错误,但已经尝试了几种方法,结果总是一样的

1 个答案:

答案 0 :(得分:0)

确定!我找到了问题并且我修理了。

在我的函数中检查是否存在

Public Function _checkAnaNoGrupo(ByVal _grupoA As Integer, ByVal _codAna As String) As Boolean

    _tabCheckAnaNoGrupo()

    Dim _ck As Boolean = False

    For Each _tRow As DataRow In _tabCheckAnaNoGrupo.Rows
        If _tRow("FK_ID_GRUPO_ANA").ToString.Contains(_grupoA) And _tRow("ID_ANA").ToString.Contains(_codAna) Then
**ck = True**

**Return _ck**

**Exit Function**

'i exit the function when he found a match, else will do the for , and the _ck value will be always false.
'So when find the first match exit the function and return TRUE

        Else
            _ck = False
        End If
    Next

    Return _ck

End Function

我的ENIGMA已解决