无法将多部分标识符绑定到C#和SQL

本文关键字:SQL 绑定 标识符 多部 | 更新日期: 2023-09-27 18:20:56

我正在尝试为WCF服务执行SQL查询,因为我使用的数据库没有返回所需数据的存储过程。您可以看到下面的代码。我正在使用VS2010并连接到Microsoft SQL Server。我得到的异常是:无法绑定多部分标识符。谢谢你的帮助。

        public List<ObjectCodeGroup> GetObjectCodeGroup()
    {
        TCDataDataContext dc16 = new TCDataDataContext();
        List<ObjectCodeGroup> results = new List<ObjectCodeGroup>();
        var objectCodeResults = dc16.ExecuteQuery<ObjectCodeGroup>(@"select t1.codeid," +
            "t1.code, t1.catalogtype, t1.codegroup, t1.codetext, t1.codegrouptext, t1.codedesc, t1.state_id from CODES t1" +
            "inner join ( select MIN(codeid) codeid, codegroup from CODES group by codegroup)" +
                "t2 on t1.codeid = t2.codeid and t1.codegroup = t2.codegroup WHERE catalogtype = 11");
        foreach (ObjectCodeGroup o in objectCodeResults)
        {
            results.Add(new ObjectCodeGroup()
           {
               codegrouptext = o.codegrouptext
           });
        }
        return results;
    }

无法将多部分标识符绑定到C#和SQL

您缺少了一堆空格,所以这是问题的一部分。添加空格(在行的末尾)!

var objectCodeResults = dc16.ExecuteQuery<ObjectCodeGroup>(@"select t1.codeid, " +
"t1.code, t1.catalogtype, t1.codegroup, t1.codetext, t1.codegrouptext, t1.codedesc, t1.state_id from CODES t1 " +
"inner join ( select MIN(codeid) codeid, codegroup from CODES group by codegroup) " +
"t2 on t1.codeid = t2.codeid and t1.codegroup = t2.codegroup WHERE catalogtype = 11");

您最初的查询类似于SELECT BLAH FROM CODES t1inner join (MOREBLAH)...。具体来看t1inner。这就是混淆这个问题的原因。