用C#写一个自增序号的程序,实现对序号的自动增加
答案:3 悬赏:30 手机版
解决时间 2021-04-18 06:01
- 提问者网友:棒棒糖
- 2021-04-17 13:22
用C#写一个自增序号的程序,实现对序号的自动增加
最佳答案
- 五星知识达人网友:山有枢
- 2021-04-17 13:41
给你个例子参考下:
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;
}
全部回答
- 1楼网友:一把行者刀
- 2021-04-17 16:11
都是自己总结的
- 2楼网友:雾月
- 2021-04-17 14:45
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我给你写的方法
{
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我给你写的方法
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯