Program Application Scenario:
1, the data table additions and deletions to increase the data, for the primary key is worth automatic generation
2, the bank's automatic station-to-station system
Here is the program code:
private void Codechange ()
{
//Set the format of the serial number in advance;
String onecode = DateTime.Now.ToString ("yyyyMMdd") + "P";
Find if the serial number in this format exists in the database
var mimi = _context.info.where (P = p.code.startswith (Onecode));
Reading database data
Determine if the code in the Info table has data
if (Mimi. Count () > 0)
{
var mm = Mimi. OrderByDescending (p = p.code); Sort by serial number in descending order
Info data = mm. First ();
int codeflow = Convert.ToInt32 (data. Code.substring (data. code.length-3));//Intercept string
String codenow = Onecode + (Codeflow + 1). ToString ("000");//combine to generate a new serial number
TextBox1.Text = Codenow;
Info ss = new info ();
Ss. Code = Codenow;
_context.info.insertonsubmit (ss);
_context. SubmitChanges ();
}
If this is the first time the query
Else
{
int i = 1;
String codenoow = Onecode + i.tostring ("000"); //Generate a string of the specified number of digits
TextBox1.Text = Codenoow;
Info ss = new info ();
Submit to Database
Ss. Code = Codenoow;
_context.info.insertonsubmit (ss);
_context. SubmitChanges ();
}
}
Generates a serial number in the specified format