Ver Mensaje Individual
  #5 (permalink)  
Antiguo 08/10/2012, 15:51
Avatar de cristiantorres
cristiantorres
 
Fecha de Ingreso: marzo-2012
Mensajes: 383
Antigüedad: 12 años, 1 mes
Puntos: 61
Respuesta: No permitir gravar datos repetidos en tablas de SQL

Cita:
Iniciado por jlcruz Ver Mensaje
OK LOS CODIGOS ESTAN BIEN, PERO YO NECESITO HACERLO EN VISUAL ESTUDIO, QUE VISUAL STUDIO ME VALIDE SI EL NUMERO DE CARNET EXISTE QUE NO LO INSERTE. ESTE ES EL CODIGO QUE ESTOY USANDO PARA GRABAR EN SQL

Private Sub txtNoEmpleado_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNoEmpleado.KeyDown
If Me.txtNoEmpleado.Text = "" Then
'MsgBox("Debe ingresar su número de empleado", MsgBoxStyle.Information)
Me.txtNoEmpleado.Focus()

Exit Sub
End If
If e.KeyCode = Keys.Enter Then

Dim strSQL As String

Me.conexion.Open()
Dim drd_asistencia As SqlDataReader

strSQL = "SET DATEFORMAT DMY INSERT INTO ASISTENCIA "
strSQL = strSQL & "(NUMERO_EMPLEADO, FECHA, HORA_ENTRADA) "

strSQL = strSQL & "VALUES (" & "'" & txtNoEmpleado.Text & "',"
strSQL = strSQL & "'" & CDate(Me.lblFecha.Text) & "'" & ", " & "'" & lblHora.Text & "'" & ")"

Dim Cmd As New SqlCommand(strSQL, Me.conexion)

drd_asistencia = Cmd.ExecuteReader()

Me.txtNoEmpleado.Text = ""
'MsgBox("Bienvenido a sus Labores", MsgBoxStyle.Information)

Cmd.Dispose()
Me.conexion.Close()
End If
End Sub
Pero analizaste la respuesta que te di en el link que te deje puedes encontrar el codigo en vb.net también.

Código vb.net:
Ver original
  1. Public Function Existe(ByVal id As Integer) As Boolean
  2.     Using conn As New SqlConnection("CadenaConexion")
  3.         Dim query = "SELECT COUNT(*) FROM TABLA WHERE Campo=@Id"
  4.         Dim cmd As New SqlCommand(query, conn)
  5.         cmd.Parameters.AddWithValue("@Id", id)
  6.         conn.Open()
  7.  
  8.         Dim count As Integer = Convert.ToInt32(cmd.ExecuteScalar())
  9.         If count = 0 Then
  10.             Return False
  11.         Else
  12.             Return True
  13.         End If
  14.     End Using
  15. End Function

Entonces a la hora de hacer el insert arias.

Código vb.net:
Ver original
  1. If (Not Existe(Convert.ToInt32(txtid.Text))) Then
  2.     'aqui haces el insert
  3. Else
  4.    MessageBox.Show("Id Ya Existe...")
  5. End If

Nota: Visual estudio es el IDE lo que estas usando es el lenguaje vb.net.
__________________
Visita mi Blog C#, vb.net, asp.net, sql, java y mas...
Blog Cristian Torres