为什么执行插入语句后该命令会获得@@Identity?
只是尝试执行插入,然后获取@@Identity,但事实证明有点困难。
所以我有以下 Sql:
int retInt = 0;
string Sql = insertSql;
Connection.Open();
commandArg.CommandText = insertSql;
commandArg.Connection = Connection;
commandArg.ExecuteNonQuery();
commandArg.CommandText = "SELECT @@Identity;";
commandArg.CommandText = Sql;
retInt = (int)commandArg.ExecuteScalar();
return retInt;
但是当我最后到达 ExecuteScalar 命令时,我得到一个空引用异常。我对 csharp 做错了什么吗?因为当我在 Sql 编辑器上执行此操作时,它可以工作(当我在线执行这两个命令时)。
希望有人可以提供帮助....
(顺便说一句,commandArg 作为参数传入)
更新:
我原来的帖子不正确,但我已经更改,但仍然收到错误。希望有人可以帮忙!
更新2:
读完下面的内容后,我可以看到我的代码很草率,所以我将代码修复为这个并且它有效!...
SqlCeConnection myConnection = new SqlCeConnection(@"Data Source=C:\database.sdf");
SqlCeCommand myCommand = new SqlCeCommand();
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.CommandText = "Insert into tbl_media (dateadded) values (getdate())";
SqlCeDataReader insertReader = myCommand.ExecuteReader();
myCommand.CommandText = "select @@identity";
SqlCeDataReader identityReader = myCommand.ExecuteReader();
myConnection.Close();
Just trying to perform an insert and then get the @@Identity but its proving a little difficult.
So I have the following Sql:
int retInt = 0;
string Sql = insertSql;
Connection.Open();
commandArg.CommandText = insertSql;
commandArg.Connection = Connection;
commandArg.ExecuteNonQuery();
commandArg.CommandText = "SELECT @@Identity;";
commandArg.CommandText = Sql;
retInt = (int)commandArg.ExecuteScalar();
return retInt;
But when I get to the ExecuteScalar command at the end I get a null reference exception.Am I doing something wrong with the csharp? Because when I do this on a Sql editor it works (when I execute both commands in line).
Hope someone can help....
(btw commandArg is passed in as a parameter)
UPDATE:
my original post was not right but I have changed and am still getting the error. Hope someone can help!
UPDATE 2:
After reading the below I can see my code is just plain sloppy so I fixed the code to be this and it works!...
SqlCeConnection myConnection = new SqlCeConnection(@"Data Source=C:\database.sdf");
SqlCeCommand myCommand = new SqlCeCommand();
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.CommandText = "Insert into tbl_media (dateadded) values (getdate())";
SqlCeDataReader insertReader = myCommand.ExecuteReader();
myCommand.CommandText = "select @@identity";
SqlCeDataReader identityReader = myCommand.ExecuteReader();
myConnection.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我一直在单独的查询中使用@@identity。重要的是它位于同一个连接上。正如之前指出的,您没有执行“SELECT @@Identity;”。您将值设置为 commandArg.CommandText = Sql;在您执行标量之前。使用 retInt = commandArg.ExecuteNonQuery();并查看是否 retInt = 1。将其放入 try catch finally 块中,以便您可以读取异常消息。这是有道理的,你在最后一行得到一个空值,因为插入不是一个标量命令。
I use @@identity all the time in a separate query. All that matters is that it is on the same connection. As pointed out before you are NOT executing "SELECT @@Identity;". You set the value to commandArg.CommandText = Sql; before you ExecuteScalar. Use retInt = commandArg.ExecuteNonQuery(); and see if retInt = 1. Put this in a try catch finally block to you can read the exception message. It makes sense you are getting a null on the last line as the insert is not a Scalar command.
听起来您正在尝试获取最近插入的项目的标识列。
鉴于:
您可以执行插入,然后查询适合插入记录的最后一条记录:
Sounds like you're trying to get the identity column for the item recently inserted.
Given that :
You could execute your insert, and then query for the last record that fits your inserted record:
将 SELECT @@IDENTITY 文本放在 insertSql 字符串的末尾。 @@IDENTITY 仅在插入后直接起作用。它不能位于不同的查询中。
Put your SELECT @@IDENTITY text at the end of your insertSql string. @@IDENTITY only works directly after an insert. It can't be in a different query.