c#SQL语句'。'附近的语法不正确

时间:2015-12-10 12:17:02

标签: c# asp.net

如果appsv = YES或NO,则此代码无法正常显示所有广告。如果用户在'approv'列中有YES,则应该检查UserInfo,然后显示他的广告,否则不显示。所以我试图解决它,但现在我已经把自己弄得更麻烦,因为我收到了错误消息,如我的帖子标题incorrect syntax near '.'

SqlDataAdapter dashowadsoneLogo = new SqlDataAdapter(@"SELECT AD.[AdsID], AD.[Country], AD.[State], AD.[City], AD.[AdsTit], SUBSTRING([AdsDesc],1,50) as AD.AdsDesc,
                  AD.[AdsPrice], AD.[Section], AD.[Category], AD.[Img1], AD.[AdsDate], I.[approv] FROM [ads] as AD JOIN UserInfo as I ON  AD.[Country] = @Location and I.[approv]='YES' ORDER BY AD.[AdsDate] DESC", cs);

4 个答案:

答案 0 :(得分:4)

您不能在列名别名中包含句点符号: -

 SUBSTRING([AdsDesc],1,50) as AD.AdsDesc,

将其更改为: -

 SUBSTRING([AD.AdsDesc],1,50) as AdsDesc,

此外,对于此类复杂查询,您应该创建stored procedure

答案 1 :(得分:0)

你有错误在这里替换你的查询  SUBSTRING([AdsDesc],1,50) as AD.AdsDesc 改成 SUBSTRING([AD.AdsDesc],1,50) as [AdsDesc]

如果它不起作用,那就像这样替换

 SELECT [AD.AdsID], [AD.Country], [AD.State], [AD.City], [AD.AdsTit],

答案 2 :(得分:0)

试试这个:

  SqlDataAdapter dashowadsoneLogo = new SqlDataAdapter(@"SELECT AD.[AdsID], AD.[Country], AD.[State], AD.[City], AD.[AdsTit], SUBSTRING(AD.[AdsDesc],1,50) as AD.AdsDesc,AD.[AdsPrice], AD.[Section],AD.[Category],AD.[Img1], AD.[AdsDate], I.[approv] FROM [ads] as AD JOIN UserInfo as I ON  AD.[Country] = @Location and I.[approv]='YES' ORDER BY AD.[AdsDate] DESC",cs);

答案 3 :(得分:0)

您错过了代码中的别名 AD

试用此代码:

SqlDataAdapter dashowadsoneLogo = new SqlDataAdapter(@"SELECT AD.[AdsID], AD.[Country], AD.[State], AD.[City], AD.[AdsTit], SUBSTRING([AD.AdsDesc],1,50) as AD.AdsDesc,
              AD.[AdsPrice], AD.[Section], AD.[Category], AD.[Img1], AD.[AdsDate], I.[approv] FROM [ads] as AD JOIN UserInfo as I ON  AD.[Country] = @Location and I.[approv]='YES' ORDER BY AD.[AdsDate] DESC", cs);

代码中的错误部分:

SUBSTRING([AdsDesc],1,50) as AD.AdsDesc