C#编写SQL查询

时间:2013-07-31 14:04:55

标签: c# sql

我正在尝试编写自定义查询并继续返回

  

SqlException未处理`error' - '。'

附近的语法不正确

代码:

var list = dbcontext.Database.SqlQuery<string>("SELECT a.AssetID, a.AssetName, a.Seg1_Code, d.ShortName, e.OfficePercentage, e.MaintenancePercentage" +
           "FROM [Core].[dbo].[Asset] a" +
           "LEFT OUTER JOIN [Core].[dbo].[AssetAddress] b" +
           "ON a.AssetID = b.AssetID" +
           "LEFT OUTER JOIN [Core].[dbo].[Address] c" +
           "ON b.AddressID = c.AddressID" +
           "LEFT OUTER JOIN [Core].[dbo].[StateLookup] d" +
           "ON c.StateID = d.StateID" +
           "INNER JOIN [Core].[dbo].[AssetPayrollMarkupOverride] e" +
           "ON a.AssetID = e.AssetID" +
           "ORDER BY d.ShortName, a.AssetName").ToList();

1 个答案:

答案 0 :(得分:4)

在串联中的每个字符串之前/之后添加空格。

 var list = dbcontext.Database.SqlQuery<string>(
                    "SELECT a.AssetID, a.AssetName, a.Seg1_Code, d.ShortName, e.OfficePercentage, e.MaintenancePercentage" +
                    " FROM [Core].[dbo].[Asset] a" +
                    " LEFT OUTER JOIN [Core].[dbo].[AssetAddress] b" +
                    " ON a.AssetID = b.AssetID" +
                    " LEFT OUTER JOIN [Core].[dbo].[Address] c" +
                    " ON b.AddressID = c.AddressID" +
                    " LEFT OUTER JOIN [Core].[dbo].[StateLookup] d" +
                    " ON c.StateID = d.StateID" +
                    " INNER JOIN [Core].[dbo].[AssetPayrollMarkupOverride] e" +
                    " ON a.AssetID = e.AssetID" +
                    " ORDER BY d.ShortName, a.AssetName").ToList();

(在上面的代码中,我在新字符串开头之前添加了空格)