自動生成編號(B開頭后跟6位,數據庫查詢不重復)


 1 private string GetAccountNo()
 2         {
 3             try
 4             {
 5                 string shortName="B";
 6                 string latestAccountNO = shortName + "000001";
            //查詢數據庫 7位且包含“B”
7 var templist = twRep.GetAll().Where(x => x.AccountNo.Length == 7 && x.AccountNo.Contains(shortName)).OrderByDescending(x => x.AccountNo).ToList(); 8 if (templist.Count>0) 9 {
             //取第一個
10 string latestNO = templist.FirstOrDefault().AccountNo; 11 12             //截取后六位 13 latestAccountNO = latestNO.Substring(1, 6);
             //判斷是否能轉換整形
14 int iNewNo = 0; 15 int.TryParse(latestAccountNO,out iNewNo);
16 if (iNewNo > 0) 17 { 18 latestAccountNO = shortName + (iNewNo + 1).ToString("000000"); 19 } 20 else 21 { 22 latestAccountNO = shortName + "000001"; 23 } 24 25 } 26 return latestAccountNO; 27 } 28 catch (Exception ex) 29 {
           //錯誤信息日志方法
30 LogHelper.WriteLog(ex.Message, ex); 31 return ""; 32 } 33 34 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM