我在c#中创建了一个表单,我需要自动生成ID,当用户点击清除数据按钮时,该ID已自动显示。而且ID的格式应该是LEL5-序列号(序列号总是在增加的情况下,如1,2,3,..)
Ex:我的第一张iD是LEL5-1,
我的第二个记录是LEL5-2
请帮忙。 谢谢。
static int EJDS_id = 1;
string code;
private void button1_Click_1(object sender, EventArgs e)
{
EJDS_id = EJDS_id + 1;
txtjobno.Text = "LEL5-" + EJDS_id.ToString();
}
答案 0 :(得分:0)
string test = "LEL5-1";
int xxxx = Convert.ToInt32(test.Substring(5));
xxxx++;
string result = string.Format("LEL5-{0}", xxxx);
答案 1 :(得分:0)
创建一个名为SERIAL_NUMBERS的表:
create table SERIAL_NUMBERS(
ID INT NOT NULL PRIMARY KEY,
LASTNUMBER INT NOT NULL);
然后插入一个字符串以存储最后一个值:
insert into SERIAL_NUMBERS(ID, LASTNUMBER) values (1, 0);
然后在后面的代码中添加以下命名空间:
using System.Data;
using System.Data.OleDb;
using System.Web.Configuration;
然后在你的代码背后:
protected void button1_Click_1(object sender, EventArgs e) {
UpdateLastNumber();
txtjobno.Text = "LEL5-" + GetLastNumber().ToString();
}
protected int GetLastNumber(){
int last = 0;
OleDbConnection con = new OleDbConnection(WebConfigurationManager.ConnectionStrings["YourConnectionStringnameFromWebConfigFile"].ConnectionString);
OleDbCommand cmd = new OleDbCommand("select lastnumber from serial_numbers where id = 1", con); // I added the condition just to be sure that only one value will be queried
con.Open();
last = Convert.ToInt32(cmd.ExecuteScalar().ToString());
con.Close();
return last;
}
protected void UpdateLastNumber(){
OleDbConnection con = new OleDbConnection(WebConfigurationManager.ConnectionStrings["YourConnectionStringnameFromWebConfigFile"].ConnectionString);
OleDbCommand cmd = new OleDbCommand("update serial_numbers set lastnumber = lastnumber + 1 where id = 1", con); // I added the condition just to be sure that only one value will be queried
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}