Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone...

let me explain what im doing...

1. read a csv file..
2. store in sql...
3. if the csv file changed@updated..then read again and store in sql

issue..

in my database..im using 3 PK ..which (L_DateTime for date, L_direction for string, L_CardID for string and L_GateID for integer)

so..when step 1 executed..there will be data in db..
when step 3 executed..i need to check for validity of PK first(this is my actual problem) and merge it into db...

im using merge command in c#..but it returns error...

this is my sample of code..

VB
string mergeSql = "merge into logDetail as Target " +
                                 "using #templogDetail as Source " +
"on " +
"Target.L_DateTime=Source.L_DateTime, " +                                                "Target.L_Direction=Source.L_Direction, " +                                                 "Target.L_CardID=Source.L_CardID " + "when matched then " +
"update set Target.L_GateID=Source.L_GateID " +"when not matched then " +
"insert (L_DateTime,L_Direction,L_CardID,L_GateID) " +                                     "values(Source.L_DateTime,Source.L_Direction,Source.L_CardID,Source.L_GateID);";

                               cmd.CommandText = mergeSql;
                               cmd.ExecuteNonQuery();


the debugger said : Incorrect syntax " , " at system.data.sqlclient

guide me friendo!
Posted

The ON clause is a single condition - I suspect you want to replace the commas with AND or OR operators.
 
Share this answer
 
Comments
Wendelius 24-Dec-11 3:46am    
You beat me to it, 5 :)
beh7606 24-Dec-11 7:14am    
both of you rocks! thanks...this is my first time application actually...thanks for your time ORiginalGriff...merry xmas..hohoho..
OriginalGriff 24-Dec-11 7:27am    
You're welcome - have a good holiday!
In the search condition use AND instead of commas. Also it is easier to read the statements if you use @ quoted literals. So perhaps something like:
C#
string mergeSql = @"
merge into logDetail as Target 
   using #templogDetail as Source
      on Target.L_DateTime=Source.L_DateTime
      and Target.L_Direction=Source.L_Direction
      and Target.L_CardID=Source.L_CardID 
when matched then
   update set Target.L_GateID=Source.L_GateID 
when not matched then
   insert (L_DateTime,L_Direction,L_CardID,L_GateID)
   values(Source.L_DateTime,Source.L_Direction,Source.L_CardID,Source.L_GateID);";
 
Share this answer
 
Comments
beh7606 24-Dec-11 7:13am    
hey mika..i realized you help people a lots...thanks for sharing with community..! thanks friend..god bless you
Wendelius 24-Dec-11 8:52am    
Thanks, that was really nicely said :) All the best to you too!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900