Ado.net ExecuteScalar() 返回 null

发布于 2024-10-07 00:50:55 字数 1715 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2024-10-14 00:50:55

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.

紫瑟鸿黎 2024-10-14 00:50:55

尝试更改存储过程以使用 Select 语句 返回标识,而不是使用这样的返回:

SELECT CAST(scope_identity() AS int)

因此将您的过程更改为:

 PROCEDURE [dbo].[AddNewPerson]
            @FirstName varchar(50)
           ,@LastName varchar(50) 
AS
BEGIN
    SET NOCOUNT ON;

INSERT INTO [People].[dbo].[Persons]
           (
            [FirstName]
           ,[LastName]
            )
     VALUES
           (
            @FirstName 
           ,@LastName 
           ) 
SELECT CAST(scope_identity() AS int)
end

来自 MSDN 上的 ExecuteScalar() 它表示它将返回第一列的第一列结果集中的行,如果结果集为空,则返回 null。

Try changing the Stored Procedure to use a Select Statement to return the identity instead of using a return like this:

SELECT CAST(scope_identity() AS int)

Thus changing your procedure to:

 PROCEDURE [dbo].[AddNewPerson]
            @FirstName varchar(50)
           ,@LastName varchar(50) 
AS
BEGIN
    SET NOCOUNT ON;

INSERT INTO [People].[dbo].[Persons]
           (
            [FirstName]
           ,[LastName]
            )
     VALUES
           (
            @FirstName 
           ,@LastName 
           ) 
SELECT CAST(scope_identity() AS int)
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文