12.23流水账号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
public TextDataContext context = new TextDataContext();
protected void Page_Load(object sender, EventArgs e)
{

if(!IsPostBack)
{


}

}

//顺序添加流水号
protected void Button1_Click(object sender, EventArgs e)
{
//去除textbox里面的值
string name = TextBox1.Text;
decimal price = Convert.ToDecimal(TextBox2.Text);
string code="";


//查询表中数据若无数据
if (context.ShangPin.Count() == 0)
{

code = DateTime.Now.ToString("yyyyMMdd") + "001";
}
else//查询表中数据若有数据

{ string max = context.ShangPin.Max(p => p.Code);
string max1 = max.Substring(8,3);

string rq = max.Substring(0, 8);
if (rq == DateTime.Now.ToString("yyyyMMdd"))
{

int max2 = Convert.ToInt32(max1);
max2 = max2 + 1;

if (max2 < 10)
{
code = DateTime.Now.ToString("yyyyMMdd") + "00" + max2;
}
else if (max2 >= 10 && max2 < 100)
{
code = DateTime.Now.ToString("yyyyMMdd") + "0" + max2;
}
else
{
code = DateTime.Now.ToString("yyyyMMdd") + max2;
}

}
else
{
code = DateTime.Now.ToString("yyyyMMdd") + "001";
}

}
//造对象
ShangPin data = new ShangPin();
data.Code = code;
data.Name = name;
data.Price = price;

//提交改动
context.ShangPin.InsertOnSubmit(data);
context.SubmitChanges();
//清空数据
Clear();

}
//清空textbox中的数据
public void Clear()
{
TextBox1.Text = "";
TextBox2.Text = "";
}


//随机添加流水号
protected void Button2_Click(object sender, EventArgs e)
{
//取出textbox里面的值
string name = TextBox1.Text;
decimal price = Convert.ToDecimal(TextBox2.Text);
Random rd = new Random();

string code = DateTime.Now.ToString("yyyyMMddhhmmss")+rd.Next(100);

//造对象
ShangPin data = new ShangPin();
data.Code = code;
data.Name = name;
data.Price = price;

//提交改动
context.ShangPin.InsertOnSubmit(data);
context.SubmitChanges();
//清空数据
Clear();
}
}

posted @ 2015-12-23 09:58  小热包1  阅读(155)  评论(0编辑  收藏  举报