validation in database
-
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.
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
-
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.
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.