I have a table in my database say Tab1(col1, col2)
entries are(corresponding entries)
Col1 col2
a - sep1
b - sep2
c - sep3
d - sep4
e - sep5
now if i upload new entries like
Col1 col2
a - sep3
b - sep2
so if this new entries does not exits then it simply INSERT these new entries else update it.
I want C# logic for it,
I tried this but its not working
if (isWellIdExists(impexp.WellId) && isUploadedDateExists(impexp.UploadedDate))
{
int result = ExecuteQuery("UpdateImpExp");
}
else
{
int result = ExecuteQuery("InsertImpExpData",
SerialIDParameter,
WellIdParameter,
);
}
Loading

Jignesh TrivediPosted Sep 10, 2014, 12:43 AM
Hi,
Record is exists in DB or not it depending on your primary (unique record)key.
so I guess here col1 and col2 are required to pickup unique record....
are you using entity frame work?
if (isWellIdExists(col1,col2))
{
//update
}
else
{
//insert
}
public bool isWellIdExists( string col1, string col2)
{
using(var context = new yourentitiobject())
{
return context.Tab1.Any(p=> p.Col1==col1 && p.Col2 == col2);
}
}
hope this will help you.
Ramesh MaruthiPosted Sep 9, 2014, 6:01 PM
first try to get all the distinct pairs you have in the database
var query = _context.yourtable.Select( p =>String.Concat(p.col1, "+" p.col2)).Distinct().ToList();
var yournewentries = String.Concat(p.col1, "+" p.col2).ToList();
var notindatabase = yournewentries.Except(query);
var in database =yournewentries.Intersect(query);
now you got the two things existed ones and notextsted ones
Abhishek JaiswalPosted Sep 9, 2014, 12:59 PM
:)
Nitesh KejriwalPosted Sep 9, 2014, 12:49 PM
CREATE PROCEDURE sp1
@Col1 nvarchar(50),@Col2 nvarchar(50)
AS
BEGIN
IF EXISTS(SELECT ID FROM Table1 WHERE Col1=@col1 AND Col2=@Col2)
BEGIN
UPDATE Table1 SET Col1=@Col1 AND Col2=@Col2
END
ELSE
BEGIN
INSERT Table1(Col1,Col2) VALUEs(@Col1,@Col2)
END
END
Jay SPosted Sep 9, 2014, 12:16 PM
Pass through new entries, query to see if it exists or not and they do an IF statement in the SQL to decide if you insert or update.