当前位置 - 股票行情交易網 - 金融財經 - C#中如何實現編號的自增,明白人請看

C#中如何實現編號的自增,明白人請看

是用C#? 那最好是用流水號生成的方法了.

給妳個例子參考下:

public string GetAutoDocNo()

{

string DocNo = "AD";

string today = DateTime.Today.Date.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);

DataSet DocDs = GetDataSet(" select * from News");

//初始化

if (DocDs.Tables[0].Rows.Count == 0)

{

DocNo += today + "101";

return DocNo;

}

/***********************/

else if (DocDs.Tables[0].Rows.Count > 0)

{

int count = 0;

string oldDocNo = string.Empty;

for (int i = 0; i < DocDs.Tables[0].Rows.Count; i++)

{

oldDocNo = DocDs.Tables[0].Rows[i]["NewsID"].ToString();

oldDocNo = oldDocNo.Substring(2, 8);

if (oldDocNo == today) count++;

}

if (count == 0/*當天還沒有單*/)

{

DocNo += today + "1001";//前面有1比較方便,沒有1在後面做的時候要稍微再加幾行代碼

return DocNo;

}

else if (count > 0/*當天已經有單*/)

{

DocDs = GetDataSet("select MAX(CAST(SUBSTRING(NewsID,3,12) AS BIGINT)) as NewsID from News where

(SUBSTRING(NewsID,3,8))='" + today + "'");

string id = DocDs.Tables[0].Rows[0]["NewsId"].ToString();

string lastid = id.Substring(8, 3);

try

{

decimal lastidec = decimal.Parse(lastid);

lastidec += 1;

return DocNo + today + (lastidec.ToString());

}

catch (Exception)

{

throw;

}