问题描述
例如,我的员工表有一个像empId,EmpName这样的列,它有5条记录。
Ex:EmpId = 1,2,3,4,5和EmpName = a,b,c ,d,e。
我必须在两个文本框中输入员工详细信息的值(empId,empName)。
这里我为EmpId设置了主键。
EmpId必须在txtEmpId.Text中自动为6。
怎么可能?
亲切的帮助我
For example i have employee table which has column like empId,EmpName it has 5 records.
Ex:EmpId=1,2,3,4,5 and EmpName=a, b,c,d,e.
And i have to two text-box to Enter value of Employee details(empId,empName).
Here i have Set primary key for EmpId.
EmpId has to come as 6 in txtEmpId.Text automatically.
How its possible?
Kindly help me
推荐答案
CREATE TABLE Employees
(
EmpID INT IDENTITY(1,1),
EmpName NVARCHAR(30)
);
之后,您需要 [],比您可以调用它。有关详细信息,请参阅: []
After that, you need to create stored procedure[^], Than you'll be able to call it. For further information, please see: How to call SQL Server stored procedures[^]
Try this,
con.open();
sqlcommand cmd=new sqlcommand("select MAX(empId)+1 from employee",con);
sqldatareader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
txtEmpId.Text= dr[0].ToString();
dr.Close();
}
if you are using empId as identity,You can just show the next empId value in textbox(txtEmpId) hence you can't insert textbox(txtEmpId) value to you employee table,
so be alert while inserting employee name alone in your table,
insert into employee(EmpName) values('A')
if You failed to insert employee name alone,
then you should have error like this,
Cannot insert explicit value for identity column in table 'emplee(your table name)' when IDENTITY_INSERT is set to OFF.
这篇关于如何将查询值分配到本地变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!