执行中自动插入ID

本文关键字:插入 ID 执行 | 更新日期: 2023-09-27 18:27:28

我在Visual studio 2010中创建了插入员工详细信息。当我单击"添加"时,它显示为已添加,id在后端自动递增。但是,如果我在执行时不得不在前端显示为("你的EMP ID是某个数字(例如10)"),我该怎么办?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRMSController;
using HRMSBusinessEntities;
using System.Data;
namespace HumanResourceManagementSystems
{
    public partial class HRMSEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string username = (string)(Session["LoginUser"]);
            if (Session["LoginUser"] != null)
            {
                lblDisplay.Text = "Welcome..." + username;
            }
            Calendar1.Visible = false;
            Label1.Visible = false;
            txtcurrdate.Text = System.DateTime.Now.ToLongDateString();
        }
        protected void Btnmodified_Click(object sender, EventArgs e)
        {
            Calendar1.Visible = true;
            
        }
        protected void Add_Click(object sender, EventArgs e)
        {
            EmployeeEntity record = new EmployeeEntity
            {
                name = txtname.Text,
                currentdate = Convert.ToDateTime(txtcurrdate.Text),
                modifieddate = Convert.ToDateTime(txtmodifieddate.Text)
            };
            EmployeeController add = new EmployeeController();
            add.Add(record);
            Label1.Visible = true;
            Label1.Text = "Added" ;
            Clear();
        }
        public void Clear()
        {
            txtname.Text = "";
            txtcurrdate.Text = "";
            txtmodifieddate.Text = "";
        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            txtmodifieddate.Text = Calendar1.SelectedDate.ToString();
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            Clear();
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Calendar1.Visible = true;
        }
    }
}

执行中自动插入ID

添加记录后,您可以调出最大id记录并在UI 中显示该记录

查询可能看起来像

Select * from Employee ORDER BY EmpId DESC LIMIT 1;

根据性能问题的建议,我们可以进行

Select max(EmpId) from Employee;

编辑

add.Add(record);
SqlConnection con = new SqlConnection(ConnString);
con.Open();
IDbCommand Cmd = con.CreateCommand();
Cmd.CommandText = "Select max(EmpId) from Employee";
IDataReader LastID = Cmd.ExecuteReader();
LastID.Read();
Label1.Visible = true;
Label1.Text = Your Empl ID is " + LastID[0].ToString() ;
Clear();
con.Close();

另一种方式可能是

protected void Add_Click(object sender, EventArgs e)
{
    EmployeeEntity record = new EmployeeEntity
    {
        name = txtname.Text,
        currentdate = Convert.ToDateTime(txtcurrdate.Text),
        modifieddate = Convert.ToDateTime(txtmodifieddate.Text)
    };
    EmployeeController add = new EmployeeController();
    add.Add(record);
    Label1.Visible = true;
    Label1.Text = "Your Empl ID is " + FetchLastID();
    Clear();
}
public string FetchLastID()
{
    SqlConnection con = new SqlConnection(ConnString);
    con.Open();
    IDbCommand Cmd = con.CreateCommand();
    Cmd.CommandText = "Select max(EmpId) from Employee";
    IDataReader LastID = Cmd.ExecuteReader();
    LastID.Read();
    return LastID[0].ToString() ;
}