Check if A CONSTRAINT UNIQUE already exists in a table? Visual Basic .NET Access

0

I'm in Visual Basic trying to make a program to manage a database, well I have the following SQL statement in the program to add UNIQUE to two fields of a table:

"ALTER TABLE RESEARCHERS ADD CONSTRAINT RESEARCHERS UNIQUE (resEmail, 
resPhoneNumber);"

But I want to make sure that before adding it, check if it already exists, since otherwise the program gives an error.

    
asked by Pedro López 27.04.2017 в 13:59
source

1 answer

0

You need to make a prior consultation, or make the attempt of INSERT (which I assume is what you want to do) and check if there is an error:

Try
    ; Tu instrucción a la BD (ExecuteNonQuery())
Catch ex As SqlException
    MessageBox.Show(ex.Message, _
        "Error en la base de datos", _
        MessageBoxButtons.OK, _
        MessageBoxIcon.Exclamation,
End Try

I recommend this because you avoid an additional query to the database in case everything is fine, and if an error happens you can easily inform the user about it.

    
answered by 27.04.2017 в 17:36