sql字符串无法在C中执行 [英] sql string cannot executed in C

查看:46
本文介绍了sql字符串无法在C中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...

让我解释一下我在做什么...

1.读取一个csv文件..
2.存储在sql ...
3.如果csv文件已更改@ updated..n,则再次读取并存储在sql

问题..

在我的数据库.im中使用3个PK ..(其中L_DateTime表示日期,L_direction表示字符串,L_CardID表示字符串,L_GateID表示整数)

所以..当执行第1步时..db中将有数据..
当执行第3步时..i首先需要检查PK的有效性(这是我的实际问题)并将其合并到db ...

我在c#中使用merge命令..但是返回错误...

这是我的代码示例..

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..

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();



调试器说: system.data.sqlclient上的语法不正确



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

guide me friendo!

推荐答案

ON子句是一个条件-我怀疑您想用AND或OR运算符替换逗号.
The ON clause is a single condition - I suspect you want to replace the commas with AND or OR operators.


在搜索条件中,使用AND代替逗号.另外,如果使用@引号文字,则更易于阅读语句.所以也许像这样:
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:
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);";


这篇关于sql字符串无法在C中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆