2014-09-04 41 views
1
 private string GenerateID() 
     { 


     } 
     private void auto() 
     { 
      AdmissionNo.Text = "A-" + GenerateID(); 

     } 

低於 A-0001 A-0002甲等的前綴等以生成序列號。如何使用C#在窗口應用

+0

類似於:http://stackoverflow.com/questions/19958518/auto-generation-of-a-sequence-number-in-c-sharp-or -SQL?RQ = 1? – jbutler483 2014-09-04 10:48:08

+0

我不滿足這個答案,沒有像我的概率那樣的解決方案。 – user3925057 2014-09-04 10:49:55

+0

你的系統中有數據庫嗎?或永久保存變量的方法? – jbutler483 2014-09-04 10:51:56

回答

0

您可以使用下面的代碼。

 `private string GenerateID() { 
     int lastAddedId = 8;// get this value from database 
     string demo = Convert.ToString(lastAddedId+1).PadLeft(4, '0'); 
     return demo; 
     // it will return 0009 

    } 
    private void auto() 
    { 
     AdmissionNo.Text = "A-" + GenerateID(); 
     // here it will set the text as "A-0009" 

    } 
1

看這個

public class Program 
{ 
    private static int _globalSequence; 

    static void Main(string[] args) 
    { 
     _globalSequence = 0; 

     for (int i = 0; i < 10; i++) 
     { 
      Randomize(i); 
      Console.WriteLine("----------------------------------------->"); 
     } 


     Console.ReadLine(); 
    } 

    static void Randomize(int seed) 
    { 
     Random r = new Random(); 
     if (_globalSequence == 0) _globalSequence = r.Next(); 

     Console.WriteLine("Random: {0}", _globalSequence); 
     int localSequence = Interlocked.Increment(ref _globalSequence); 

     Console.WriteLine("Increment: {0}, Output: {1}", _globalSequence, localSequence); 
    } 

} 
相關問題