这是我的插入代码:
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
我的问题是:如果我运行这样的代码,插入语句可以正常工作,但重复行。
但是,如果我使用放置在“谜”中的 _query 运行代码(见 UP),则 INSERT 什么也不做。没有错误 - 没有
我希望你能帮助我,可能是一个愚蠢的错误,但已经尝试了几种方法,结果总是一样的