c# SqlConnection 执行复杂的sql语句出错

jack2684 2012-03-17 11:01:23
c#执行如下语句,出错。无论用nonqueryexcute还是readerexcute都不行


string strSql = @"declare @type char declare @id bigint set @id = select max(Sid) from Stream set @type = select StreamType from Stream where Sid=@id if @type = 'a' then select Talk.Talk,UserOfUp.Latitude,UserOfUp.Longtitude from UserOfUp,Talk,Stream where UserOfUp.UserId = 1 and Stream.StreamId = Talk.TalkId and Stream.Sid = @id end ";

---------------------------------
error:关键字 'select' 附近有语法错误。 关键字 'select' 附近有语法错误。 关键字 'then' 附近有语法错误。 'end' 附近有语法错误。
...全文
141 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
色拉油 2012-03-17
  • 打赏
  • 举报
回复
断点调试,看看你生成的SQL语句是什么,拿到数据库里执行,看看到底哪里出错了[Quote=引用 4 楼 rekym 的回复:]

if @type = 'a'
select Talk.Talk,UserOfUp.Latitude,UserOfUp.Longtitude from UserOfUp,Talk,Stream where UserOfUp.UserId = 1 and Stream.StreamId = Talk.TalkId and Stream.Sid = @id";

//改成这样  不要 then……
[/Quote]
dalmeeme 2012-03-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chinajiyong 的回复:]
引用 1 楼 jack2684 的回复:

上面有点乱,改一下
C# code

string strSql = @"declare @type char
declare @id bigint
set @id = select max(Sid) from Stream
set @type = select StreamType from Stream where Sid=@id
……
[/Quote]
同意,改成存储过程执行比较好。
EnForGrass 2012-03-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jack2684 的回复:]

上面有点乱,改一下
C# code

string strSql = @"declare @type char
declare @id bigint
set @id = select max(Sid) from Stream
set @type = select StreamType from Stream where Sid=@id
if @type = 'a' then sele……
[/Quote]
在sql版回答了
declare @type char
declare @id bigint
set @id = (select max(Sid) from Stream)
set @type = (select StreamType from Stream where Sid=@id)
if @type = 'a' then
(select Talk.Talk,UserOfUp.Latitude,UserOfUp.Longtitude from UserOfUp,Talk,Stream where UserOfUp.UserId = 1 and Stream.StreamId = Talk.TalkId and Stream.Sid = @id)
end
rekym 2012-03-17
  • 打赏
  • 举报
回复
if @type = 'a'
select Talk.Talk,UserOfUp.Latitude,UserOfUp.Longtitude from UserOfUp,Talk,Stream where UserOfUp.UserId = 1 and Stream.StreamId = Talk.TalkId and Stream.Sid = @id";

//改成这样  不要 then end 这个语法错误,LZ试下
Csharp_琪 2012-03-17
  • 打赏
  • 举报
回复
这个应该就是sql语句的错误了,你在sqlserver里面先执行下试试
EnForGrass 2012-03-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jack2684 的回复:]

上面有点乱,改一下
C# code

string strSql = @"declare @type char
declare @id bigint
set @id = select max(Sid) from Stream
set @type = select StreamType from Stream where Sid=@id
if @type = 'a' then sele……
[/Quote]
你最好把它写成存储过程来执行
jack2684 2012-03-17
  • 打赏
  • 举报
回复
上面有点乱,改一下

string strSql = @"declare @type char
declare @id bigint
set @id = select max(Sid) from Stream
set @type = select StreamType from Stream where Sid=@id
if @type = 'a' then select Talk.Talk,UserOfUp.Latitude,UserOfUp.Longtitude from UserOfUp,Talk,Stream where UserOfUp.UserId = 1 and Stream.StreamId = Talk.TalkId and Stream.Sid = @id
end ";

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧