0

I have a table in (local database c#) which has two columns (name1 and name2) and i have already inserted names in both columns. now i need to check if any similar name in the two columns exist or not. if a name exists, just show mw a MessageBox.Show("this name exist"); How can i do that? Thanks.

Jamal Salman
  • 199
  • 1
  • 10
Flash
  • 85
  • 10
  • you can write something like this select count(last_name) frm tbl where last_name in (select first_name from tbl) .. Or you can also use intersection of two columns – Vivek Nuna Oct 03 '19 at 09:21
  • It is not clear what you are asking. Would you like to compare the existing name1 and name2 columns or do you have a new value that you would like to compare to all the values name1 and name2? What have you tried? – mortb Oct 03 '19 at 09:26
  • I need only to compare the existing name1 and name2 columns – Flash Oct 03 '19 at 09:28

2 Answers2

0

There is already a subject close to your question :

Sql: How to properly check if a record exists

And I do think SQL count return 0 "if no rows" so you just have to make a simple condition to show your MessageBox().

Edit: if you want to check for similar name apply the same verification manipulation to both of your columns then compare both count.

(Sorry I'm french my English is approximate)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mrproex
  • 9
  • 5
  • The link which you sent belongs to MSql-server. I need two compare two columns in C#. and i don't know how to do it – Flash Oct 03 '19 at 09:33
  • Ow you dont know how to make sql request in c# ? Let me know if im right y will edit my answer. – Mrproex Oct 03 '19 at 09:59
  • 1) I have a column in C# local database which named "name1".i already inserted 10 names into it. 2) i have also a "textbox2". when i enter a name in this text box it saves the new name in second column which named "name2" 3) now i want to compare the recent value from "name2" column to all values in "name1" column. 4)if the recent name, existed in "name1" then showMessagebox("exists") – Flash Oct 03 '19 at 10:09
0

Still not sure what it is that you exactly want. This sql will list all name1 that have an equal name2:

SELECT name1 FROM theTable t1
WHERE name1 IN (
  SELECT name2 FROM theTable)
)

Of course you need to change theTable to the real name of your table.

If you want to run the SQL in C# you can follow the documentation. For example here: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader

mortb
  • 9,361
  • 3
  • 26
  • 44