SqlException was unhandled



请给我建议,我有代码,在执行过程中显示此异常:

SqlException未处理。无法绑定多部分标识符"T.TerritoryDescription"
无法绑定由多部分组成的标识符"T.TerritoryID"
无法绑定由多部分组成的标识符"T.TerritoryDescription"。

当我在ManagementStudio中运行SQL查询时,我会得到一些结果。当我在Visual Studio中使用此脚本运行代码时,我在这行代码上遇到异常

reader = command.ExecuteReader();

这是我的代码:

public void databaseQuestion()
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
    SqlCommand command = 
       new SqlCommand((@"select T.TerritoryID,T.TerritoryDescription,avg(O.Freight)
                         from Employees E
                         inner join EmployeeTerritories ET on E.EmployeeID = ET.EmployeeID
                         inner join Orders O on E.EmployeeID = O.EmployeeID
                         where T.TerritoryDescription ='Bedford'
                         group by T.TerritoryID,T.TerritoryDescription,O.Freight") ,con);
    dbFile = filePath + @"sqlFile.txt";
    SqlDataReader reader;
    con.Open();
    reader = command.ExecuteReader();
    using (StreamWriter file = new StreamWriter(dbFile, false))
    {
        while (reader.Read())
        {
            file.WriteLine(reader["T.TerritoryID"] + "t" + reader["T.TerritoryDescription"]+ "t" +reader["O.Freight"]);
        }
    }
    reader.Close();
    con.Close();
}     

错误,无法绑定多部分标识符XXXX。表示如果XXXX由表的别名组成,则应检查是否已为同一查询中的表定义了该别名。在您的场景中,您使用的是一个名为"T"的别名,该别名从未为查询中的任何表定义过(您的别名E表示Employee,O表示Orders,ET表示EmploymeTerries。)

相关内容

最新更新