如何将查询值分配到本地变量? [英] How Can I Assign Query Value Into Local Variable?

查看:88
本文介绍了如何将查询值分配到本地变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


这篇关于如何将查询值分配到本地变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆