自动生成不重复的ID,利用2分查找,生成中间缺少的ID值,保持id的连续性

 #region  生成不重复的Id

        /// <summary>

        /// 根据参数生成一个在表内指定字段的唯一字符串

        /// </summary>

        /// <param name="length">字符串长度</param>

        /// <param name="tableName">表名</param>

        /// <param name="keyFieldName">字段</param>

        /// <returns>返回长度为length的字符串</returns>

        private static OracleDataAdapter oraAdp = null;

        public string GenerateKey(int length, string tableName, string keyFieldName)

        {

            // return "";

            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["gdfsys"];

            string connectionString = settings.ConnectionString;

            if (oraAdp == null)

            {

                oraAdp = new OracleDataAdapter("select 1 from dual", connectionString);

            }

            oraAdp.SelectCommand.CommandText = "select " + keyFieldName + " from " + tableName + " order by " + keyFieldName;

            DataTable dt = new DataTable();

            oraAdp.Fill(dt);

 

            // 使用二分法查找

            int lower = 1;

            // 没有数据则默认为1

            if (dt != null && dt.Rows.Count > 0)

            {

                // 如果最后一条的ID等于记录数,则说明中间没有删除的ID,

                if (Convert36To10(dt.Rows[dt.Rows.Count - 1][0].ToString()) == dt.Rows.Count)

                {

                    lower = dt.Rows.Count;

                }

                else

                {

                    int upper = dt.Rows.Count - 1;

                    while (lower <= upper)

                    {

                        int m = (lower + upper) / 2;

                        if (m > 0 && Convert36To10(dt.Rows[m - 1][0].ToString()) > m)

                        {

                            upper = m - 1;

                        }

                        else

                        {

                            lower = m + 1;

                        }

                    }

                    lower = upper;

                }

                lower++;

            }

            return GetKey(length, Convert10To36(lower));

        }

        public string Convert10To36(int num)

        {

            string value = "";

            if (num == 0)

            {

                value = "0";

            }

            else

            {

                while (num > 0)

                {

                    int n = num % 36;

                    value = GetCharValue(n) + value;

                    num = num - n;

                    if (num > 0)

                    {

                        num = num / 36;

                    }

                }

            }

            return value;

        }

        private string GetCharValue(int num)

        {

            return num.ToString();

        }

        public string GetKey(int length, string num)

        {

            while (num.Length < length)

            {

                num = "0" + num;

            }

            return num;

        }

        public int Convert36To10(string num)

        {

            int value = 0;

            for (int i = 0; i < num.Length; i++)

            {

                //value = value + GetIntValue(num[num.Length - 1 - i]) * ((int)Math.Pow(10, i));

                //value += GetIntValue(num[num.Length - 1 - i]);

                value += GetIntValue(num[num.Length - 1 - i]) * ((int)Math.Pow(10, i));

            }

            return value;

        }

        private int GetIntValue(char num)

        {

            int GetValue = 0;

            if (num <= '9')

            {

                GetValue = int.Parse(num.ToString());

            }

            return GetValue;

        }

        #endregion

posted @ 2014-07-23 17:02  闫小彪  阅读(247)  评论(0编辑  收藏  举报