Getting duplicate key values ​​from SQL Exception

As you know, SQL Server 2008+ gives us an idea of ​​what values ​​are duplicated by saying

Duplicate Key Value (foo, bar)

in the SqlException message. I usually get these values ​​using a regular expression and getting data between parentheses to show them to end users.

I wonder; is there a more elegant way to get these values?

+6
source share
2 answers

Do not think that there is an elegant way, I would handle it the way you do with RegEx.

+1
source

this is a sample ... this is to get the value of the specified field and compare with textBox1 (here the user inserts what they want) ...

 oleDbConnection1.open(); string query = "select * from database"; OleDbCommand comand = new OleDbCommand(query,oleDbConnection1); OleDbDataReader reader = comand.ExecuteReader(); reader.Read(); string value = reader.getValue(1).ToString(); reader.Close(); if(textBox1.Text == value) { MessageBox.Show("Data Dublicate","Error"); } 
-3
source

Source: https://habr.com/ru/post/954228/


All Articles