0
请给我建议,我有一些代码,在其执行过程中显示此异常:SQLEXCEPTION了未处理
SQLEXCEPTION了未处理的多部分标识符“T.TerritoryDescription”无法绑定。
无法绑定多部分标识符“T.TerritoryID”。
无法绑定多部分标识符“T.TerritoryDescription”。
当我在Management Studio中运行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();
}
您没有表格智慧你的sql语句中的别名T.如果你先在SqlServer Management Studio中运行sql语句,那么你可以修复它。 – rene 2014-10-04 12:24:18
是的,看起来像你没有加入你的表,领土(或任何实际的名字是) – 2014-10-04 12:26:10