来自SQL Server的ExecuteNonQuery和返回值,C#

时间:2011-03-24 06:29:25

标签: c# sql-server

我想为用户提供一个Web服务,以便在表中插入或更新某些值。用户输入ID和三个参数。如果数据库中不存在ID,我想返回0,失败或类似。当我测试下面的代码并提供一个不存在的ID时,存储过程返回1个单元格(返回值)为0但状态设置为1.我想这是因为我在执行查询时使用ToString()它返回1个单元格。那么我该如何改进下面的代码?

我在方法中有这个代码:

string status = "";

    SqlParameter[] param = { 
        new SqlParameter("@ID", ID),
        new SqlParameter("@Flag", Flag),
        new SqlParameter("@C_Interval", C_Interval),
        new SqlParameter("@D_Interval", D_Interval)
    };

    status = DatabaseHelper.ExecuteNonQuery("sp_InsertInstruction", param);

return status;

在ExecuteNonQuery中,我将存储过程命令传递给数据库

// skipped some of the code this example
    status = cmd.ExecuteNonQuery().ToString();
    return status;

我的存储过程如下:

ALTER PROCEDURE dbo.sp_InsertInstruction
@Flag char(1) = null,
@C_Interval int=null,
@D_Interval int=null,
@ID char(20),

AS

DECLARE @Entity_ID int
SET @Entity_ID = (SELECT ID FROM Entity WHERE ID = @ID)

INSERT INTO Instructions(Flag, C_Interval, D_Interval, Entity_ID)
VALUES (@Flag, @C_Interval, @D_Interval, @Entity_ID)

提前致谢。

3 个答案:

答案 0 :(得分:1)

ExecuteNonQuery返回受影响的行数。在这种情况下,您总是插入一行。

将您的SP更改为以下内容:

ALTER PROCEDURE dbo.sp_InsertInstruction
    @Flag char(1) = null,
    @C_Interval int=null,
    @D_Interval int=null,
    @ID char(20),

AS

DECLARE @Entity_ID int
SET @Entity_ID = (SELECT ID FROM Entity WHERE ID = @ID)

IF (@Entity_ID IS NOT NULL)
BEGIN
    INSERT INTO Instructions(Flag, C_Interval, D_Interval, Entity_ID)
    VALUES (@Flag, @C_Interval, @D_Interval, @Entity_ID)
END

即。仅在实体ID确实存在时才插入。

答案 1 :(得分:0)

将此代码放在存储过程的顶部

ALTER PROCEDURE dbo.sp_InsertInstruction
@Flag char(1) = null,
@C_Interval int=null,
@D_Interval int=null,
@ID char(20),

AS

    IF NOT EXISTS
    (
        SELECT ID 
        FROM Entity 
        WHERE ID = @ID
    )
    BEGIN
        RETURN 0;
    END

-- Your remaining SQL Code here....

答案 2 :(得分:0)

返回的值显示为“1”是执行查询后返回的总行数。

以下是您可能需要执行的操作的草稿,这只是关于您的过程中的数据如何的概念 对代码进行处理。我正在修改你的sql过程,以便返回你期望的值。

对于守则背后:

using System.Data.SqlClient;
using System.Data;

public class DBAccess{
    private string strCon = "Data Source=YourServer;Initial Catalog=YourDBName;etc";
    public string GetResult(){

        string strQuery = "Exec YourProcedure Param1";
        SqlCommand cmdSql = new SqlCommand(strQuery);
        SqlConnection conSql = new SqlConnection(strCon);
        conSql.Open();
        cmdSql.Connection=conSql;
        SqlDataReader dreSql = cmdSql.ExecuteReader();
        dreSql.Read();
        // here I'm trying to read the item of the row on the column named Result
        return dreSql["Result"].ToString();

    }
}

您的程序:

ALTER PROCEDURE dbo.sp_InsertInstruction   
    @Flag char(1) = null,   
    @C_Interval int=null,   
    @D_Interval int=null,   
    @ID char(20)      AS      

    DECLARE @Entity_ID int   
    SET @Entity_ID = (SELECT ID FROM Entity WHERE ID = @ID)      

    if(@Entity_ID is not null)
        begin
            INSERT INTO Instructions(Flag, C_Interval, D_Interval, Entity_ID)   
            VALUES (@Flag, @C_Interval, @D_Interval, @Entity_ID)  

            -- this will return as a table with the result of 1
            select Result='1'
        end
    else
        begin
            -- this will return as a table with the result of 0
            select Result='0'
        end