如何每天生成账单编号从1开始 [英] How to generate bill number every day starts from 1

查看:419
本文介绍了如何每天生成账单编号从1开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#2010 windows应用程序创建Windows应用程序,在我的应用程序中我使用下面的代码生成帐单编号,但在这里我想知道如何从每天开始我的帐单编号1.给我任何一个想法。



i am creating windows application using c# 2010 windows application, in my application i am using below code for bill number generation, but here i want how to my bill number every day starts from 1. give me any one some ideas.

public void AutoNumber()
{

SqlConnection con = new SqlConnection(db.Connectionstring());
con.Open();

SqlCommand cmd = new SqlCommand("SELECT max(billno) +1 FROM billing", con);

SqlDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{

while (dr.Read())
{
lblbillno.Text = dr[0].ToString();

if (lblbillno.Text == "")
{
lblbillno.Text = "1";
}


}
}

else
{
lblbillno.Text = "1";

}
con.Close();
}





我的尝试:





What I have tried:

How to generate bill number every day starts from 1

推荐答案

你必须存储最后一个账单号的日期。然后,如果这样的日期与当前日期不匹配,请使用 billno = 1 重启。
You have to store the date of the last bill number. Then if such a date doesn't match with the current one, restart with billno=1.


永远不要这样做。虽然它可能看起来工作正常,但在生产环境中它会导致灾难,因为多个用户最终会获得相同的价值,并且您的整个数据库很快就会变得无法理解。



在数据库中使用IDENTITY列,让SQL对唯一数字进行排序。然后,您可以使用子查询通过使用PARTITION BY每天返回增量值:

Never ever do that. while it may appear to work fine, in a production environment it is a recipe for disaster as multiple users end up with the same value and your entire database very quickly becomes corrupted beyond belief.

Use an IDENTITY column in your DB, and let SQL sort out unique numbers. You can then use a subquery to return a incremental value per day by using PARTITION BY:
SELECT ROW_NUMBER() OVER (PARTITION BY EnterDate  ORDER BY RowIDValueColumn ASC) AS DailyNumber, * 
FROM MyTable 
WHERE EnterDate >= '2016-02-02' 
ORDER BY RowIDValueColumn ASC


As OriginalGriff已经说过,你没有。让SQL服务器管理它。

另一方面,如果您的账单号码除了正在运行的号码之外还有某种格式,您可能需要看看这个向您的数据库询问该唯一ID [ ^ ]。
As OriginalGriff has said, you don't. Let the SQL server manage it.
On the other hand, if your bill number has a certain format besides a running number, you may want to take a look at this Ask Your Database for that Unique ID[^].


这篇关于如何每天生成账单编号从1开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆