Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. validation in database

validation in database

Scheduled Pinned Locked Moved LINQ
database
3 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nilish
    wrote on last edited by
    #1

    i have a table named table1 , having two fields id and name . Before inserting the record into the table , how should i find out whether the record i am going to insert . exist ot not.

    H P 2 Replies Last reply
    0
    • N Nilish

      i have a table named table1 , having two fields id and name . Before inserting the record into the table , how should i find out whether the record i am going to insert . exist ot not.

      H Offline
      H Offline
      Howard Richards
      wrote on last edited by
      #2

      I am assuming you are using LINQ to SQL, and that id is the primary key:

      var newID = "ID";
      var newName = "name";

      var db = new MyDataContext();
      var exists = (db.Table1.Where( t => t.ID == ID).Count() != 0);

      // this will return exists as a boolean for your table
      // it would be best implemented as a static method on your data context..

      'Howard

      1 Reply Last reply
      0
      • N Nilish

        i have a table named table1 , having two fields id and name . Before inserting the record into the table , how should i find out whether the record i am going to insert . exist ot not.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        That's a tough one, because between you checking for uniqueness and you actually inserting your record, somebody else could insert the same record. Ways that this can be achieved would normally be outside the scope of typical LINQ operations. For instance, one way would be to do an insert with the check as part of the condition, e.g. insert into mytable(name) select @name where not exists ....

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups