我有以下网络服务:
using (SqlCommand cmd = new SqlCommand(@"SELECT r.NAME, s.NAME FROM REGION r LEFT OUTER JOIN STADT s ON s.REGION_ID = r.ID ORDER BY r.NAME", con))
{
con.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
if (rdr["r.NAME"] != DBNull.Value && rdr["s.NAME"] != DBNull.Value)
{
stadtObject.Add(new STADT()
{
RegionName = rdr["r.NAME"].ToString(),
StadtName = rdr["s.NAME"].ToString()
});
}
}
}
}
我在SQL Server Management Studio中测试了 SQL 语句,它的工作就像一个魅力。但是,如果我在浏览器中调用该方法,则会出现错误:
System.IndexOutOfRangeException: r.NAME
at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
at System.Data.SqlClient.SqlDataReader.get_Item(String name)
at StadtHelper.Stadt() in C:\Users\Yeah\Documents\Visual Studio 2010\Projects\WebService1\WebService1\StadtHelper.cs:line 31
at WebService1.Service1.Stadt() in C:\Depp\Ushi\Documents\Visual Studio 2010\Projects\WebService1\WebService1\Service1.asmx.cs:line 77
我究竟做错了什么?