繁体   English   中英

从SQL Server C#执行ExecuteNonQuery和返回值

[英]ExecuteNonQuery and Return Value from SQL Server, C#

我想为用户提供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)

提前致谢。

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确实存在时才插入。

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

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....

被显示为“ 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

暂无
暂无

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

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