请给我建议,我有代码,在其执行过程中显示此异常:
SqlException未被处理,多部分标识符"T.TerritoryDescription“无法绑定。 多部分标识符"T.TerritoryID“无法绑定。 多部分标识符"T.TerritoryDescription“无法绑定。
当我在Management中运行SQL查询时,会得到一些结果。在Visual中使用此脚本运行代码时,这一行代码会出现异常
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();
} 发布于 2015-04-08 12:34:53
无法绑定多部分标识符XXXX的错误。如果XXXX由表的别名组成,则应检查是否已为同一查询中的表定义了此别名。在您的场景中,您使用的别名名为"T“,它从未为查询中的任何表定义过(您的别名E表示Employee,O表示Orders,ET用于EmployeeTerritories)。
https://stackoverflow.com/questions/26192775
复制相似问题