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


Jay S159 reputationPosted Sep 9, 2014, 8:16 AM EDT
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.
Nitesh KejriwalRank #253 · 9.9K reputationPosted Sep 9, 2014, 8:49 AM EDT
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
Abhishek JaiswalRank #126 · 17.5K reputationPosted Sep 9, 2014, 8:59 AM EDT
:)
Ramesh MaruthiRank #937 · 2.2K reputationPosted Sep 9, 2014, 2:01 PM EDT
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
Jignesh TrivediRank #16 · 50.3K reputationPosted Sep 9, 2014, 8:43 PM EDT
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.