如何将查询值分配到本地变量?

编程入门 行业动态 更新时间:2024-10-23 17:26:06
本文介绍了如何将查询值分配到本地变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

例如,我的员工表有一个像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.

更多推荐

如何将查询值分配到本地变量?

本文发布于:2023-11-30 01:20:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   如何将   分配

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!