从多个表中选择名称并使用相同的ID进行排序

时间:2012-06-06 14:22:03

标签: c# json sql-server-2008

我有问题。我有2个表,看起来像这样:

表1:国家/地区

Name: Germany
ID: 1

Name: France
ID: 2

表2:城市

Name: Frankfurt
ID: 1

Name: Paris
ID: 2

我想写一个select语句来比较id并将城市与具有相同id的正确国家进行排序。输出应采用json格式。

到目前为止,我的代码看起来像这样:

public class StadtHelper
{
   public class STADT
   {
       public string StadtName { get; set; }
       public string RegionID { get; set; }
   }

   internal static List<STADT> Stadt()
   {
       List<STADT> stadtObject = new List<STADT>();

       using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
       using (SqlCommand cmd = new SqlCommand(@"SELECT NAME, REGION_ID FROM STADT",con))
       {
           con.Open();
           using (SqlDataReader rdr = cmd.ExecuteReader())
           {
               while (rdr.Read())
               {
                   if (rdr["NAME"] != DBNull.Value && rdr["REGION_ID"] != DBNull.Value)
                   {
                       stadtObject.Add(new STADT()
                       {
                           StadtName = rdr["NAME"].ToString(),
                           RegionID = rdr["REGION_ID"].ToString()
                       });
                   }
               }
           }
       }

       return stadtObject;
   }
}

提前致谢

我编辑了我的代码:

    public class StadtHelper
{
    public class STADT
    {
        public string StadtName { get; set; }
        public string RegionName { get; set; }
    }
    internal static List<STADT> Stadt()
    {
        List<STADT> stadtObject = new List<STADT>();

        using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
        using (SqlCommand cmd = new SqlCommand(@"SELECT REGION.NAME, STADT.NAME FROM REGION, STADT WHERE REGION.ID = STADT.REGION_ID ORDER BY REGION.NAME" , con))
        {
            con.Open();
            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    if (rdr["REGION.NAME"] != DBNull.Value && rdr["STADT.NAME"] != DBNull.Value)
                    {
                        stadtObject.Add(new STADT()
                        {
                            RegionName = rdr["REGION.NAME"].ToString(),
                            StadtName = rdr["STADT"].ToString()
                        });
                    }
                }
            }
        }
        return stadtObject;
    }
}

还有一个错误,我的网络服务给了我一个例外:

 System.IndexOutOfRangeException: REGION.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:\Users\Yeah\Documents\Visual Studio 2010\Projects\WebService1\WebService1\Service1.asmx.cs:line 77

1 个答案:

答案 0 :(得分:1)

鉴于你的表,TSQL看起来像,

    select ci.Name, co.Name
      from Cities  ci
inner join Country co on co.ID = ci.ID
  order by ci.Name, co.Name