日期:2014-05-17 浏览次数:20523 次
using (SqlConnection conn = new SqlConnection("连接字符串")) { SqlCommand cmd = new SqlCommand("select replace(列名,'号','') as col1 from 表名", conn); conn.Open(); List<int> list = new List<int>(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { list.Add(Convert.ToInt32(sdr["col1"])); } } if (list.Count > 0) { list.Sort(); bool[] array = new bool[list[list.Count - 1]]; foreach (int i in list) array[i - 1] = true; Console.WriteLine("没有的号有:"); for (int i = 0; i < array.Length; i++) { if (!array[i]) Console.WriteLine(i + 1); } } }
------解决方案--------------------
处理成数字数组,然后循环比较看有没有呗,最笨的办法了
string str = "1号,3号,6号"; str=str.Replace("号", ""); int[] arr = str.Split(',').OrderBy( p=>p ).Select(p1=>int.Parse(p1)).ToArray(); List<int> result = new System.Collections.Generic.List<int>(); for (int i = arr[0]; i < arr[arr.Length - 1]; i++) { if (!arr.Contains(i)) { result.Add(i); } }
------解决方案--------------------
static void Main(string[] args) { string[] arry = { "1号", "3号", "5号", "10号","100号" }; List<string> list = new List<string>(); int len = int.Parse(arry.Last().Substring(0, arry.Length - 2)); for (int i = 1; i < len; i++) { if (!arry.Contains(i.ToString()+"号")) { list.Add(i.ToString()); } } foreach (var item in list) { Console.WriteLine(item); } Console.Read(); }
------解决方案--------------------
1,用SQL把数字查出来。 select replace(column,'号','') as col1 from table
2,用一个数组保存这个结果
3,弄一个嵌套循环,如果array[i] <>i,则取出所有 array[i]和i之间的值。