当前位置: 首页>>代码示例>>C#>>正文


C# Section.Select方法代码示例

本文整理汇总了C#中Section.Select方法的典型用法代码示例。如果您正苦于以下问题:C# Section.Select方法的具体用法?C# Section.Select怎么用?C# Section.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Section的用法示例。


在下文中一共展示了Section.Select方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetInstruments

 /// <summary>
 /// Gets a list of Instrument objects filtered by a section
 /// </summary>
 /// <param name="selectedSection">A section to filter instruments by</param>
 /// <returns></returns>
 //public List<Instrument> GetInstruments(Section selectedSection)
 //{
 //    using (dbconn = new SqlConnection(Music.Properties.Settings.Default.orchestraConnection))
 //    {
 //        dbconn.Open();
 //        dbcomm = new SqlCommand(String.Format("select Instrument, Type from Instruments where Type = '{0}' order by Type asc",selectedSection.ToString()), dbconn);
 //        dbreader = dbcomm.ExecuteReader();
 //        List<Instrument> instruments = new List<Instrument>();
 //        while (dbreader.Read())
 //        {
 //            string instrumentName = dbreader["Instrument"].ToString();
 //            Section instrumentType;
 //            if (Enum.TryParse<Section>(dbreader["Type"].ToString(), true, out instrumentType))
 //                instruments.Add(new Instrument(instrumentName, instrumentType));
 //            else
 //                instruments.Add(new Instrument());
 //        }
 //        dbconn.Close();
 //        return instruments;
 //    }
 //}
 /// <summary>
 /// Gets a list of Instrument objects filtered by an array of Sections
 /// </summary>
 /// <param name="selectedSection">An array to filter instruments by</param>
 /// <returns></returns>
 public List<Instrument> GetInstruments(Section[] selectedSection)
 {
     try
     {
         using (dbconn = new SqlConnection(Music.Properties.Settings.Default.orchestraConnection))
         {
             dbconn.Open();
             //This command creates a sql string that checks for instruments of a type contained in the parameter list which is created by using a string join and some string formats
             dbcomm = new SqlCommand(String.Format("select Instrument, Type from Instruments where Type in({0}) order by Instrument asc", String.Join(",", selectedSection.Select(x => String.Format("'{0}'", x.ToString())).ToArray())), dbconn);
             dbreader = dbcomm.ExecuteReader();
             List<Instrument> instruments = new List<Instrument>();
             while (dbreader.Read())
             {
                 string instrumentName = dbreader["Instrument"].ToString();
                 Section instrumentType;
                 //This sneaky little section tries to parse the instrument value of the instrument by using the Enum.TryParse which returns a bool, if false there is a bad type and a default instrument replaces it
                 if (Enum.TryParse<Section>(dbreader["Type"].ToString(), true, out instrumentType))
                     instruments.Add(new Instrument(instrumentName, instrumentType));
                 else
                     instruments.Add(new Instrument());
             }
             return instruments;
         }
     }
     catch
     {
         throw new ArgumentException("Something has gone horribly, horribly wrong. Most likely my SQL instance is unreachable or down.");
     }
 }
开发者ID:vanhouc,项目名称:vanhouc-final,代码行数:60,代码来源:DataRepository.cs


注:本文中的Section.Select方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。