Foreground HTML code:
<%@ page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%><! DOCTYPE html> Background C # code:
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{protected void Page_Load (object sender, E Ventargs e) {} protected void Button3_Click (object sender, EventArgs e) {//Fetch value string name = Te Xtbox1.text; Decimal Price = Convert.todecimal (TextBox2.Text); Testdatacontext context = new Testdatacontext (); String code = ""; The format of the serial number takes the current date time to take out his month//check the commodity table there is no code if (context. Shangpin.count () = = 0) {code = DateTime.Now.ToString ("yyyyMMdd") + "001"; } else {//finds the Database Code column with the largest data interception after three bits +1 string max = context. Shangpin.max (P=>p.code); Intercepts string max1 = max. Substring (8,3); Intercept string, RQ = max. Substring (8); if (RQ = = DateTime.Now.ToString ("YyyyMMdd"))If you want to wait the same day {//turn int max2 = Convert.ToInt32 (MAX1);//If it is executed the same day MAX2 = max2 + 1; if (Max2 <) {code = DateTime.Now.ToString ("yyyyMMdd") + "XX" + max2; } else if (Max2 <) {code = DateTime.Now.ToString ("YyyyMMdd") + "0" + max2; } else {code = DateTime.Now.ToString ("yyyyMMdd") + max2; }}//execute to this else//not the same day restart {code = DateTime.Now.ToStrin G ("YyyyMMdd") + "001"; }}//Add data first create a Shangpin object shangpin data = new Shangpin (); Assigns the value data. Code = code; Data. name = name; Data. Price = Price; Context. Shangpin.insertonsubmit (data); Context. SubmitChanges (); } protected void Button4_Click (objECT sender, EventArgs e) {testdatacontext context = new Testdatacontext (); String name = TextBox1.Text; Decimal Price = Convert.todecimal (TextBox2.Text); A random number is defined by the randomness rd = new random (); Generates a random number of string code = DateTime.Now.ToString ("Yyyymmddhhmmss") +rd within 1000. Next (1000); Add data first create a Shangpin object shangpin data = new Shangpin (); Assigns the value data. Code = code; Data. name = name; Data. Price = Price; Context. Shangpin.insertonsubmit (data); Context. SubmitChanges (); }}
SQL display:
WB tips generate random numbers for serial numbers