无法从表格中获得所需的记录

时间:2017-02-18 20:04:37

标签: c# sql database oop

我已经创建了一个为数据库中的记录生成新ID的函数。

通过简单地获取前一条记录的ID然后向前移动一个字母来实现此目的。例如,如果先前的ID是AAA,则新ID将是AAB,依此类推。

我获取之前ID的方法是计算表中的行数,然后使用该数字来获取表中的最后一条记录。我拿走1的原因是因为行计数器没有索引,但是记录的数量是。因此,在包含50条记录的表中,最后一条记录将记录49条。

问题是此功能仅适用于1条记录。因此,只有第一个生成的ID才会向前移动,其余的将与第二个完全相同。例如,这就是它的样子。

0 - AAA, 1 - AAB, 2 - AAB, 3 - AAB ......

// Generate codes in order
    public string StrGenerateSequentialCode(string StrTableName)
    {

        // Get the places for the counters from the previous code
        AccessTable ObjStoreCode = new AccessTable();
        ObjStoreCode.SelectTable(StrTableName);

        string StrPreviousID = "";

        StrPreviousID = ObjStoreCode.StrGetRow(ObjStoreCode.IntGetRowCount() - 1, 0);

以下是该函数的其余代码。

 char[] ArrCollection = new char[36] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        int[] ArrPreviousIDSpace = new int[11]; // the number if chars for this code is fixed to 11

        for (int i = 0; i < ArrPreviousIDSpace.Length; i++)
        {
            for (int j = 0; j < ArrCollection.Length; j++)
            {
                if (ArrCollection[j] != StrPreviousID[i])
                {
                    ArrPreviousIDSpace[i]++;
                }
                else
                {
                    break;
                }
            }
        }


        // Now generate a code with each character carrying on from the last
        string StrCode = "";


        /* Add one to the last parts position until it reaches 27, 
         * when it does set its position to 0 and then add one to the second last parts
           position and repeat the process for the third last part...*/

        int A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, G = 0, H = 0, I = 0, J = 0, K = 0;

        // Make the starting points for the char selecters the 
        A = ArrPreviousIDSpace[0];
        B = ArrPreviousIDSpace[1];
        C = ArrPreviousIDSpace[2];
        D = ArrPreviousIDSpace[3];
        E = ArrPreviousIDSpace[4];
        F = ArrPreviousIDSpace[5];
        G = ArrPreviousIDSpace[6];
        H = ArrPreviousIDSpace[7];
        I = ArrPreviousIDSpace[8];
        J = ArrPreviousIDSpace[9];
        K = ArrPreviousIDSpace[10];

        // Turn the clock
        K++;
        if (K == ArrCollection.Length)
        {
            K = 0;

            J++;
        }
        if (J == ArrCollection.Length)
        {
            J = 0;

            I++;
        }
        if (I == ArrCollection.Length)
        {
            I = 0;

            H++;
        }
        if (H == ArrCollection.Length)
        {
            H = 0;

            G++;
        }
        if (G == ArrCollection.Length)
        {
            G = 0;

            F++;
        }
        if (F == ArrCollection.Length)
        {
            F = 0;

            E++;
        }
        if (E == ArrCollection.Length)
        {
            E = 0;

            D++;
        }
        if (D == ArrCollection.Length)
        {
            D = 0;

            C++;
        }
        if (C == ArrCollection.Length)
        {
            C = 0;

            B++;
        }
        if (B == ArrCollection.Length)
        {
            B = 0;

            A++;
        }
        // Combine the chars to make a final password
        StrCode = ArrCollection[A].ToString() + ArrCollection[B].ToString() + ArrCollection[C].ToString() + ArrCollection[D].ToString() + ArrCollection[E].ToString() + ArrCollection[F].ToString() + ArrCollection[G].ToString() + ArrCollection[H].ToString() + ArrCollection[I].ToString() + ArrCollection[J].ToString() + ArrCollection[K].ToString();
        return StrCode;

这是从表中获取记录的函数。

public string StrGetRow(int IntSelectedRow = 0, int IntSelectedColumn = 0)
    {
        string StrRequesedRow = "";



        // This if statement will check whether or not the selected coloumns or rows are larger than the amount available
        if (IntSelectedRow < ObjDataSet.Tables[0].Rows.Count & IntSelectedColumn < ObjDataSet.Tables[0].Columns.Count)
        {
            // Make the table the data row origianates from the table on the dataset 
            DataRow = ObjDataSet.Tables[0].Rows[IntSelectedRow];
            // This will store the data in the string  'StrRequestedRow
            StrRequesedRow = DataRow.ItemArray.GetValue(IntSelectedColumn).ToString();
        }
        else
        {
            StrRequesedRow = "NO MORE RECORDS";
        }

        return StrRequesedRow;
    }

1 个答案:

答案 0 :(得分:1)

如果我记得我的模块化微积分,你的算法应该是这样的

 public static string NumberToString(int number, int length,char[] allChars)
        {
            var remain = number;
            var result = "";
            var total = allChars.Length;
            while (remain >= total)
            {
                result = allChars[remain % total]+result;

                remain = (int)Math.Floor(remain * 1.0 / total);
            }
            result = allChars[remain]+result;
            return result.PadLeft(length, allChars[0]);
        }

这可以优化,但很简单。你这样用它。

static void Main(string[] args)
        {

            char[] allChars = { 'A', 'B', 'C', 'D' };
            var count = 0;
            Console.WriteLine(NumberToString(count,3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(++count, 3, allChars));
            Console.WriteLine(NumberToString(30, 3, allChars));
            Console.WriteLine(NumberToString(63, 3, allChars));


            Console.WriteLine("Done");
            Console.ReadLine();
        }

只需使用行数并使用长度和字符集。 但是,不要说我没有警告你并发性。在读取行数时,您必须锁定表,直到插入新行为止。因为如果有人在那段时间插入新行,你将会遇到ID的冲突。