例如,我的员工表有一个像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
推荐答案请勿在客户端执行此操作。让数据库引擎这样做。 请阅读身份 [ ^ ]属性并将其设置为 EmpID 字段。它将自动增加 EmpID 。 最后,你必须创建表格 [ ^ ]: Do not do this on client side. Let the database engine do that. Please, read about identity[^] property and set it to EmpID field. It will increase EmpID automatically. Finally, you have to create table[^] in this way: CREATE TABLE Employees ( EmpID INT IDENTITY(1,1), EmpName NVARCHAR(30) );
之后,您需要创建存储过程 [ ^ ],比您可以调用它。有关详细信息,请参阅:如何调用SQL Server存储过程 [ ^ ]
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.
更多推荐
如何将查询值分配到本地变量?
发布评论