我的数据库中有2列intCaseID(int)和caseNo(nvarchar)。我遇到了一个逻辑问题,即如果有人提交案件,我需要照顾年月。
因此情况将不会如此(例如:20190301,假设今天是2019年3月30日)。但是,如果同一个人在2019年3月31日的另一个日期提交案件,则将是20190302.02表示案件编号。但是,如果日期为2019年4月1日,则计数器将重置为(20190401)
下面是逻辑的工作方式
protected void Button1_Click(object sender, EventArgs e)
{
string CaseNo="";
//If today is 31 March 2019, filename = 20190301. If second person
//submit on same day, filename = 20190302
//But if date is 1 April 2019, filename = 20190401
}
答案 0 :(得分:1)
由于我不知道如何连接到数据库,因此您必须完成/修复某些部分,但这会带给您大致的认识
protected void Button1_Click(object sender, EventArgs e)
{
//If today is 31 March 2019, filename = 20190301. If second person
//submit on same day, filename = 20190302
//But if date is 1 April 2019, filename = 20190401
DateTime processDate = DateTime.Now; // get current date
string caseNo = "";
string casePrefix = string.Format("{0}{1:D2}", processDate.Year, processDate.Month); // to help you search previous cases and create the new caseNo
Int16 caseInMonthNumber = 0; // Int value of the case so you can increment
string sqlResult = ""; // result from your query
string sqlString = string.Format("SELECT caseNo from TABLE WHERE caseNo LIKE '{0}%' ORDER BY caseNo DESC LIMIT 1", casePrefix); // syntax might change depending on your DB
// Function to call your database or whatever you are using to keep count of cases, this will get you the highest value so the highest case... return the caeNo value
// sqlResult = functionQueryToDB(sqlString); // or just send the prefix and create the 'sqlString' string in the function
//process the result from the database if there are no results, you don't need to do anything extra, start with caseInMonthNumber = 1,
//but if you get a result, you need to read the last part of the string and increment by one... Depending on the logic for your program you might want to add
// extra validations for the string
if ( sqlResult != "" ) {
if ( !Int16.TryParse(sqlResult.Substring(sqlResult.Length - 2) , out caseInMonthNumber) ) {
//error handling, the last two digits are not a valid number
return;
}
}
caseNo = string.Format("{0}{1:D2}", casePrefix, ++caseInMonthNumber);
// Do what you need with the new caseNo
}
您需要考虑的一些事情:
希望这会有所帮助。
致谢
编辑:您的函数无效,我在错误处理中返回了空字符串。
答案 1 :(得分:1)
达到预期结果的一种方法是,每当有人创建新案件时,都要检查数据库中该人的最新案件编号。
将当前日期的月份(即创建新案例编号的时间)与最近的案例编号进行比较。如果两个都在同一月份,那么新的案例编号就是旧的案例编号加1。否则,新的案例编号将月份值的新数字和最后一个值设置为01。