用C#写一个自增序号的程序,实现对序号的自动增加
设计一个c#函数实现对序号的自动增加,数据格式为”gc”+4位年份+2位月份+序号,下次插入数据时如果新添数据编号重复,则自动递增。如数据表中有gc2015070001记...
设计一个c#函数实现对序号的自动增加,数据格式为”gc”+4位年份+2位月份+序号,下次插入数据时如果新添数据编号重复,则自动递增。如数据表中有gc2015070001记录,下次插入时编号自动为gc2015070002
展开
2个回答
2015-07-07
展开全部
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;
}
追问
这样还是不可以啊,因为第一次添加的时候strNum是gc2015070002,如果第二次再添加的时候它还是从2015070002开始的,而不是自动添加为2015070003??
追答
//这里换成你从数据库读取的编号
string lastNo = "gc2015070001";
你没看到我写的这句注释吗?lastNo你是需要从你的数据库表中取最后一条记录的No号,不是原班不动的copy我给你写的方法
2015-07-07
展开全部
给你个例子参考下:
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;
}
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;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询