asp.net 日期流水号(急!在线等)

ysh88gg 2010-12-23 09:21:24
想写一个当新增一笔数据时能自动编号存进数据库里的程序,
而且编号需以当日新增的日期+上流水号来新增进SQL里,
(如:20006(年) 07(月) 25(日) 01(流水号) = 2006072501)10码,
而若遇上8月1日时可以自动更为(2006080101),07年1月1日也是(2007010101)

string str=DateTime.Now.ToString("yyyyMMdd")+流水号;
但是流水号怎么写。我想要这样的结果:今天是2010-12-23号(2010122301-2010122399)明天是2010-12-24(2010122401-2010122499)后面2位是流水好,随着日期变了,流水号就又从01开始++;
或者让它从01一直加到99循环。
...全文
393 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
YZX3288 2010-12-23
  • 打赏
  • 举报
回复
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(con);
string sql = string.Format("select top 1 字段名 from 表名 where 字段名 like '%{0}%' order by 字段名 desc", DateTime.Now.ToString("yyyyMM"));
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
字段名= int.Parse(DateTime.Now.ToString("yyyyMM")) * 1000 + 1;
}
else
{
字段名= int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
}
用这个例子可以实现日期的流水号....lz可以试下...
ysh88gg 2010-12-23
  • 打赏
  • 举报
回复
9楼能给个实例代码吗,谢
极地_雪狼 2010-12-23
  • 打赏
  • 举报
回复
将流水号放到数据库里,或放到配置文件里,然后再读取更新,和计数器似的。
ycproc 2010-12-23
  • 打赏
  • 举报
回复
最开始那会我做网站的一个案例 。贴给你 。不过现在看这些代码觉得很冗余
你还是在数据库用t-sql语句 来 处理 效率高点

protected void Button1_Click(object sender, EventArgs e)
{
staffModel staff = new staffModel();
staff.Staffpwd = this.pwd.Text.ToString().Trim();
string type = this.regtype.SelectedValue.ToString();
//判断选择类型
switch (type)
{
case "1":
staff.Stafftype = "员工注册";
break;
case "2":
staff.Stafftype = "教师注册";
break;
case "3":
staff.Stafftype = "学生注册";
break;
case "4":
staff.Stafftype = "家长注册";
break;
case "5":
staff.Stafftype = "渠道注册";
break;
}
staff.Staffname = this.people.Text.ToString().Trim();
staff.Staffsex = int.Parse(this.sex.SelectedValue.ToString());
staff.Staffinc = this.inc.Text.ToString().Trim();
staff.Staffbonken = this.bonken.Text.ToString().Trim();
staff.Staffphone = this.phone.Text.ToString().Trim();
staff.Stafftel = this.tel.Text.ToString().Trim();
staff.Staffmail = this.mail.Text.ToString().Trim();
staff.Staffqq = this.qq.Text.ToString().Trim();
staff.Staffaddress = this.address.Text.ToString().Trim();
staff.Staffzipcode = this.zipcode.Text.ToString().Trim();
staff.Staffnote = this.node.Text.ToString().Trim();
staff.Staffdate = System.DateTime.Now;
string year = System.DateTime.Now.ToString("yy");//截取时间年份

//查询最大流水号
DataSet ds = bll.Getmaxstaffcodeid();
string id = ds.Tables[0].Rows[0][0].ToString();

string newid;
if (id == "" && id == null)
{
newid = "00000";
}
else
{
newid = Convert.ToString(Convert.ToInt32(id) + 1).PadLeft(5, '0');
}
staff.Staffcode = type + year + newid;//生成流水号
if (this.yzm.Text.ToString().ToUpper() == this.IMGCODE1.Text)
{
if (eastelite.DIP.BLL.staffBll.addregister(staff))
{
Session["reg"] = staff;//存入session
MailMessage mail = new MailMessage();
staffModel s = Session["reg"] as staffModel;//接收session
string mailfrom = System.Configuration.ConfigurationManager.AppSettings["servermailname"].ToString();
string mailto = this.mail.Text.ToString().Trim();
string mailpwd = System.Configuration.ConfigurationManager.AppSettings["servermailpwd"].ToString();
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.To = this.mail.Text; //接受人的邮箱
mail.From = mailfrom; //你自己的邮箱(注意qq邮箱不行)
mail.Subject = this.people.Text.ToString() + ",您好,感谢您注册****公司系统帐户"; //邮箱标题
mail.Body = "尊敬的" + this.people.Text + ":<br>感谢您对于本系统的支持与关注!<br>您的注册信息如下:<br>系统帐号:<font color='red'>" + s.Staffcode + "</font><br>系统密码:<font color='red'>"
+ this.pwd.Text + "</font><br>用户名:<font color='red'>" + this.people.Text + "</font><br>安全邮箱:<font color='red'>" + this.mail.Text
+ "</font><br>帐号或其他相关问题请登录我们的官网咨询或留言.<br>谢谢您的关注!(请勿回复系统邮件)";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //stmp验证
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mailfrom);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailpwd);
SmtpMail.SmtpServer = "smtp.ym.163.com"; //企业代理邮件服务器
SmtpMail.Send(mail);
cleartext();//清空文本
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "str", "<script>window.open('regdetatile.aspx',target='_self');</script>");//弹出信息显示页面
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "str", "<script>alert('抱歉。注册提交失败。请刷新后重新尝试!')</script>");
}
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "str", "<script>alert('验证码错误。请重新填写!')</script>");
}
}

xuan.ye 2010-12-23
  • 打赏
  • 举报
回复
简单的就是弄个序号表,查询序号表,做业务的时候,更新序号表。
laowang134 2010-12-23
  • 打赏
  • 举报
回复
这得看你想要什么样的流水号了,比如中间删除了一个,此时是依然按最后的+1还是需要将删除的流水号必须补充上,如果要补充上就麻烦了,如果+1的话,就查出本日最后更新的编号取最后两位(流水号)+1,如果为空就为01,判断+1后的结果如果小于10就0+新流水号然后前面加上日期。。
kckjsoft 2010-12-23
  • 打赏
  • 举报
回复
在数据库中建立一个表其中包含两个字段:一个当天时间如:2010-12-23第二个字段写入流水号如:01,当需要打印账单时利用当前时间和第一个字段值求timespan如果天数为差值一就将第一个字段值设置为当天时间,流水号设置为01,如果天数差值为0,流水号就加一。当然数据也可以不存储在数据库中
wuyq11 2010-12-23
  • 打赏
  • 举报
回复
DateTime.Now.ToString("yyyyMMdd")+最大值.ToString().PadLeft(2,'0')或
CREATE FUNCTION f_NextBH()
RETURNS char(8)
AS
BEGIN
RETURN(SELECT 'BH'+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK))
END
GO

chinaxiaocha 2010-12-23
  • 打赏
  • 举报
回复
Dim myConnection As New OracleConnection("Data Source=****;Persist Security Info=true;User ID=*****; Password=****;Unicode=True")
myConnection.Open()
Dim strsqlB As String
strsqlB = "select nvl(max(substr(ADMINNO,7,4)),0)+1,sysdate from COMPUTERREQUESTSHEET where TO_NUMBER(TO_CHAR(PRINTDATE,'yyyyMM')) =TO_NUMBER(TO_CHAR(SYSDATE,'yyyyMM'))"
Dim cmB As New OracleCommand(strsqlB, myConnection)
Dim drB As OracleDataReader
drB = cmB.ExecuteReader()
If drB.Read Then
If Len(drB.Item(0).ToString) = 1 Then
LblNumber.Text = Format(drB.Item(1), "yyyyMM") & "000" & Trim(drB.Item(0)).ToString
ElseIf Len(drB.Item(0).ToString) = 2 Then
LblNumber.Text = Format(drB.Item(1), "yyyyMM") & "00" & Trim(drB.Item(0)).ToString
ElseIf Len(drB.Item(0).ToString) = 3 Then
LblNumber.Text = Format(drB.Item(1), "yyyyMM") & "0" & Trim(drB.Item(0)).ToString
Else
LblNumber.Text = Format(drB.Item(1), "yyyyMM") & Trim(drB.Item(0)).ToString
End If
End If
drB.Close() '"管理編號:" &
myConnection.Close()
myConnection.Dispose()

我的是最后四位是流水號,按月來記的,相信你能理解
ysh88gg 2010-12-23
  • 打赏
  • 举报
回复
能具体点嘛?我只是个新手啊,最好弄个例子参考参考,呵呵,先谢谢各位了。
yixinpai 2010-12-23
  • 打赏
  • 举报
回复
楼主,不是已经自己知道怎么办了么,就是日期加上流水号;
每增加一个新的,比较一下数据库中最大的那个跟要新增的大小关系;
如果要增加的流水号日期段比数据库中的最大记录日期段小:说明1、当前时间比实际小;2、数据库中有跳号;
如果要增加的流水号日期段比数据库中的最大记录日期段大;说明:是当天第一笔记录;
如果要增加的流水号日期段比数据库中的最大记录日期段相等;说明:最大记录流水号段+1.
ph550873328 2010-12-23
  • 打赏
  • 举报
回复
读出数据库中最后的日期+流水号,然后判断是要流水号上加1还是重新重01开始
myhope88 2010-12-23
  • 打赏
  • 举报
回复
自己弄个算法判断一下呗
laowang134 2010-12-23
  • 打赏
  • 举报
回复
这个需要读出数据库中最后的流水号。然后+1就好了吧。。。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧