Ado.net ExecuteScalar() 返回 null
我正在使用 ado.net 和 ExecuteScalar 命令在 c# 中(通过 vs2008)执行存储过程。存储过程返回输入的新记录的 pkey,但 ExecuteScalar 返回 null。我查看数据库,确实添加了一条记录。我可以使用输出参数来获取值,但我不知道为什么这不起作用。
当我在 ssms 中执行 sp 时,返回 pkey。
我做错了什么?
这是 C# 代码:
public int SaveNewPerson(EPerson ePerson)
{
int newPersonPkey;
SqlConnection cn = new SqlConnection(cnn.PersonData);
using (cn)
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "People.dbo.AddNewPerson";
cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 150).Value = ePerson.LastName;
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 150).Value = ePerson.FirstName;
cn.Open();
object result = cmd.ExecuteScalar();
newPersonPkey = int.Parse(result.ToString());
cn.Close();
}
catch (Exception e)
{
// call error method
throw new Exception(e.Message + " save new Person error ");
}
}
return newPersonPkey;
}
这是 sp:
PROCEDURE [dbo].[AddNewPerson]
@FirstName varchar(50)
,@LastName varchar(50)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [People].[dbo].[Persons]
(
[FirstName]
,[LastName]
)
VALUES
(
@FirstName
,@LastName
)
declare @persons_PKey int
set @persons_PKey = @@IDENTITY
return @persons_PKey
end
I am executing a stored procedure in c# (through vs2008) using ado.net with an ExecuteScalar command. The stored proc returns the pkey of the new record entered, but ExecuteScalar is returning null. I look in the database and a record has indeed been added. I could use an output parameter to get the value, but then I won't know why this didn't work.
When I execute the sp in ssms, the pkey is returned.
What am I doing wrong?
Here is the C# code:
public int SaveNewPerson(EPerson ePerson)
{
int newPersonPkey;
SqlConnection cn = new SqlConnection(cnn.PersonData);
using (cn)
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "People.dbo.AddNewPerson";
cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 150).Value = ePerson.LastName;
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 150).Value = ePerson.FirstName;
cn.Open();
object result = cmd.ExecuteScalar();
newPersonPkey = int.Parse(result.ToString());
cn.Close();
}
catch (Exception e)
{
// call error method
throw new Exception(e.Message + " save new Person error ");
}
}
return newPersonPkey;
}
And this is the sp:
PROCEDURE [dbo].[AddNewPerson]
@FirstName varchar(50)
,@LastName varchar(50)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [People].[dbo].[Persons]
(
[FirstName]
,[LastName]
)
VALUES
(
@FirstName
,@LastName
)
declare @persons_PKey int
set @persons_PKey = @@IDENTITY
return @persons_PKey
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ExecuteScalar
方法返回结果的第一条记录的第一个字段,但由于您的查询不会产生结果,因此它将返回 null。您可以选择该值而不是从存储过程返回该值,也可以添加一个方向设置为 ParameterDirection.ReturnValue 的参数来捕获存储过程返回的内容。
The
ExecuteScalar
method returns the first field of the first record of the result, but as your query doesn't produce a result, it will return null.You can either select the value instead of returning it from the stored procedure, or add a parameter with the direction set to
ParameterDirection.ReturnValue
to catch what the stored procedure returns.尝试更改存储过程以使用
Select 语句
返回标识,而不是使用这样的返回:因此将您的过程更改为:
来自 MSDN 上的 ExecuteScalar() 它表示它将返回第一列的第一列结果集中的行,如果结果集为空,则返回 null。
Try changing the Stored Procedure to use a
Select Statement
to return the identity instead of using a return like this:Thus changing your procedure to:
From the documentation of the ExecuteScalar() on MSDN it says that it will return the first column of the first row in the result set or null otherwise if the result set is empty.