2017-02-18 12 views
0

データベース内のレコードの新しいIDを生成する関数を作成しました。テーブルから目的のレコードを取得できません

これは、前のレコードのIDが何であるかを取得してから、それを一文字前方に移動することで行います。例えば、前のIDがAAAであれば、新しいIDはAABであり、以下同様である。

私が以前のIDを取得する方法は、テーブルの行数を数え、その数値を使ってテーブルの最後のレコードを取得することです。私が1を取り去っているのは、行カウンタが0ではなくレコード数があるからです。したがって、50レコードのテーブルでは、最後のレコードは49レコードとなります。

この関数は1レコードのみで動作するという問題があります。したがって、最初に生成されたIDだけが前方に移動し、残りのIDは第2と同じになります。たとえば、これはそのようになります。 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; 
    } 
+3

これは完全に間違っています。最新のDBMSにはレコードを挿入して新しいレコードのIDを返す方法があります。どのDBMSを使用していますか? –

+0

私はOLEDBと組み合わせてMicrosoft SQLを使用しています。また、テーブルからレコードを取得するために使用している関数を追加しました。 – Abdiyee

+0

この全体のことはちょっとばかげています。実際に自動生成されたIDを必要としない場合は、[Guid.NewGuid()](https://msdn.microsoft.com/en-us/library/system.guid.newguid(v = vs.110).aspx新しいIDやデータベースに依存しないものを生成するためのものです。あなたのやり方は、複数の理由(並行性、パフォーマンス、ロックなど)で少し悪いです。 –

答えて

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の衝突が発生するためです。

+0

SPOT ON。どうもありがとう。あなたは救い主です。 – Abdiyee

+0

@Abdi Glad私はアルゴリズムを編集して、ハードコードされていない配列からゼロ文字を読み出すのを助けることができました。これがあなたが必要とするものであれば、それを返答としてマークすることを忘れないでください。データベースの読み込みに問題が残っている場合や、番号に変換する必要がある場合は、コメントに入れて回答を編集してください。 –

関連する問題