1.
create table tb
(
ID int identity,
OrderID as replace(convert(char(10),getdate(),120),'-','')+stuff(convert(varchar,(10000+ID)),1,1,''),
OrderName nvarchar(50)
)
2.
public string GetOrderId()
{
SqlConnection con = new SqlConnection(DAL.DB.CON_STRING);
con.Open();
SqlCommand cmd = new SqlCommand("select max(OrderId) from [Order]",con);
string orderId;
string maxOrderId = Convert.ToString(cmd.ExecuteScalar());
string orderDate = maxOrderId.Remove(8,3);
string today = System.DateTime.Now.ToString("yyyyMMdd").ToString();
if(maxOrderId =="")
{
orderId = System.DateTime.Now.ToString("yyyyMMdd").ToString()+"001";
return orderId;
}
else
{
if(orderDate==today)
{
orderId = Convert.ToString(Convert.ToUInt64(maxOrderId)+1);
return orderId;
}
else
{
orderId = System.DateTime.Now.ToString("yyyyMMdd").ToString()+"001";
return orderId;
}
}
}