简体   繁体   English

从C#代码使用SQL Server CE选择@@ IDENTITY

[英]SELECT @@ IDENTITY using SQL Server CE from C# code

Im using Data Adapter/Set in SQL CE, i do create the following query to insert into table and then SELECT @@IDENTITY, I want this SELECT statement return me the Student ID each time after Inserting into table, here is my Query: 我在SQL CE中使用数据适配器/集,我确实创建了以下查询以插入到表中,然后执行SELECT @@ IDENTITY,我希望此SELECT语句在插入表后每次返回我的学生ID,这是我的查询:

INSERT INTO [Student] ([Name], [Family], [Address], [Phonenumber])
VALUES(@Name,@Family,@Address,@Phonenumber);
SELECT  @@IDENTITY;

here is how i call query: 这是我如何调用查询:

int x = da.Insert("Albert", "Alexandra", "No4.Oxford", Telnum);

Int x suppose to return me ID... Int x假设会传回我的身分证...

Here is the Error im getting : 这是错误即时消息:

There was an error parsing the query. 解析查询时出错。 [ Token line number = 4,Token line offset = 1,Token in error = SELECT ] [令牌行号= 4,令牌行偏移量= 1,令牌错误= SELECT]

Insert Query it self it works but once adding SELECT @@ IDENTITY at the end im getting error. 自我插入查询它可以正常工作,但一旦在最后添加SELECT @@ IDENTITY时出现错误。

I really don't know what i'm doing wrong. 我真的不知道我在做什么错。

The return value of ExecuteNonQuery will be number of rows effected by these query. ExecuteNonQuery的返回值将是这些查询影响的行数。 so you need to use store procedure instead of Single Query. 因此,您需要使用存储过程而不是单一查询。

根据MSDN ,CE每次执行不支持多个命令,您需要作为两个命令同步执行。

If you'd like to do this in a single call, you need to use a stored procedure rather than Insert , because it uses ExecuteNonQuery , which does not return any records. 如果要在单个调用中执行此操作,则需要使用存储过程而不是Insert ,因为它使用ExecuteNonQuery ,该函数不返回任何记录。 Otherwise you'll need to perform a select in another call to determine the identity. 否则,您将需要在另一个调用中执行选择以确定身份。

The return value of ExecuteNonQuery is an integer that denotes the number of rows affected by your call. ExecuteNonQuery的返回值是一个整数,表示受您的调用影响的行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM