Skip to main content
added 582 characters in body
Source Link
Steven Borges
  • 431
  • 1
  • 4
  • 17

If you don't want repeating IDs you'll have to set the ID as the Primary Key, which is pretty much obligatory.

If you don't want the Name to repeat, you could populate a list with the Names the table contains, and then you would only insert whatever name is not in that List.

Here is an example, instead of using a list I used a dictionary:

Dictionary<int, string> Names = new Dictionary<int, string> ();
using (SqlCommand command = new SqlCommand ("SELECT * FROM TestTable", con))
using (SqlDataReader reader = command.ExecuteReader ()) {
   while (reader.Read ()) {
      Names.Add (reader["ID"], reader["NAME"]);
   }
}

if (!Names.ContainsValue ("ValueYouWantToInsert")) {
   //do stuff
}

If you don't want repeating IDs you'll have to set the ID as the Primary Key, which is pretty much obligatory.

If you don't want the Name to repeat, you could populate a list with the Names the table contains, and then you would only insert whatever name is not in that List.

If you don't want repeating IDs you'll have to set the ID as the Primary Key, which is pretty much obligatory.

If you don't want the Name to repeat, you could populate a list with the Names the table contains, and then you would only insert whatever name is not in that List.

Here is an example, instead of using a list I used a dictionary:

Dictionary<int, string> Names = new Dictionary<int, string> ();
using (SqlCommand command = new SqlCommand ("SELECT * FROM TestTable", con))
using (SqlDataReader reader = command.ExecuteReader ()) {
   while (reader.Read ()) {
      Names.Add (reader["ID"], reader["NAME"]);
   }
}

if (!Names.ContainsValue ("ValueYouWantToInsert")) {
   //do stuff
}
Source Link
Steven Borges
  • 431
  • 1
  • 4
  • 17

If you don't want repeating IDs you'll have to set the ID as the Primary Key, which is pretty much obligatory.

If you don't want the Name to repeat, you could populate a list with the Names the table contains, and then you would only insert whatever name is not in that List.