古詩詞大全網 - 成語故事 - 用C#寫壹個自增序號的程序,實現對序號的自動增加

用C#寫壹個自增序號的程序,實現對序號的自動增加

private?string?GetNo()

{

string?no?=?"";

//這裏換成妳從數據庫讀取的編號

string?lastNo?=?"gc2015070001";

string?year?=?lastNo.Substring(2,4);

string?month?=?lastNo.Substring(6,2);

string?number?=?lastNo.Substring(8,4);

DateTime?current?=?DateTime.Now;

int?intNum?=?0;

string?strNum?=?"";

//同年同月

if?(year?==?current.ToString("yyyy")?&&?month?==?current.ToString("MM"))

{

intNum?=?Int32.Parse(number)?+?1;

strNum?=?intNum.ToString();

if?(strNum.Length?==?1)

{

strNum?=?"000"?+?strNum;

}

else?if?(strNum.Length?==?2)

{

strNum?=?"00"?+?strNum;

}

else?if?(strNum.Length?==?3)

{

strNum?=?"0"?+?strNum;

}

no?=?"gc"?+?year?+?month?+?strNum;

}

else

{

no?=?"gc"?+?current.ToString("yyyyMM")+"0001";

}

return?no;

}