尋找缺失位置

來源:互聯網
上載者:User

 資料表:ttest

id (int)              單位編號varchar(20)

    1                              00101

    1                              00102

    1                              00103

    1                              00106

    1                              00107

需要找出不連續的編號,最小的不連續編號。

這裡少了00104,00105要找出00104

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. public partial class test_getNext : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         Bind();
  17.     }
  18.     private void Bind()
  19.     {
  20.         string connStr = ConfigurationManager.ConnectionStrings["hrmbookConnectionString"].ToString();
  21.         SqlConnection conn = new SqlConnection(connStr);
  22.         DataSet ds = new DataSet();
  23.         try
  24.         {
  25.             conn.Open();
  26.             string Sqlstr = "SELECT 單位編號 FROM ttest order by 單位編號";
  27.             SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
  28.             da.Fill(ds);
  29.             Response.Write(getNextFixed(ds, 0));
  30.         }
  31.         catch (Exception ex)
  32.         {
  33.             Response.Write(ex.ToString());
  34.         }
  35.         finally
  36.         {
  37.             conn.Close();
  38.         }
  39.     }
  40.     /// <summary>
  41.     ///  01,02,04和形式,則返回03.
  42.     /// </summary>
  43.     /// <param name="ds"></param>
  44.     /// <param name="pos">判斷的列的位置</param>
  45.     /// <returns></returns>
  46.     public string getNextFixed(DataSet ds,int pos)
  47.     {
  48.         string returnCode = "01";
  49.         if (ds == null) return returnCode;
  50.         if (ds.Tables.Count == 0) return returnCode;
  51.         if (ds.Tables[0].Columns.Count == 0) return returnCode;
  52.         if (ds.Tables[0].Rows.Count == 0) return returnCode;
  53.         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  54.         {
  55.             string s = ds.Tables[0].Rows[i][pos].ToString().Trim();
  56.             if (s.Length > 2) s = s.Substring(s.Length - 2);//一個參數時,表示從哪裡開始截取,而不是長度。這裡表示最後兩位。
  57.             if (s != returnCode) break;//不為01則跳出,這種思路比較奇怪,但是這裡正好實用。
  58.             if (s == "99") return "";//不能超過三位
  59.             //格式輸出,比如:one{0},two{1}.string.Format("{0:C3}", 2)輸出$2.000
  60.             //這裡把1轉化為01
  61.             returnCode = string.Format("{0:00}", Convert.ToUInt16(s) + 1);//touint16不需要長整型
  62.         }
  63.         return returnCode;
  64.     }
  65. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.