2013-02-21 1 views
0

이 내 INSERT 코드 : 여기 나는 내 방식의 수수께끼를 가지고 확인하고 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 

작동 전 DataTable을로 SQL 테이블에서 데이터를 얻을 :

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 문을 잘 수행하지만 행은 중복됩니다.

그러나 'enigma'(UP 참조)에있는 _query를 사용하여 코드를 실행하면 INSERT는 아무 작업도 수행하지 않습니다. 오류 - 아무것도

나는 당신이 날 도울 수 있기를 바랍니다는 어리석은 실수 나하지 수 없지만 이미 여러 가지 방법을 시도하고, 결과는 항상 확인 같은

+0

를 해결 존재 여부를 확인 내 기능에

했다, 어쩌면 저장 프로 시저를 사용? 또한 디버깅하기 어려운 문제를 일으킬 수있는 많은 전역 변수를 사용하는 것 같습니다. – PatFromCanada

+0

좋습니다, 전역 변수를 사용하지 않을 것입니다. – Flavius69

답변

0

입니다 수 있습니다! 나는 문제를 발견하고 fixit. 그것은

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 

내 수수께끼가

당신은 삽입 쿼리 자체에 중복 체크를 넣어 할 수 있습니다