Generates a serial number in the specified format

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.