简体   繁体   English

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

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

I want to provide a web service for users to insert or update some values in a table. 我想为用户提供Web服务,以便用户在表中插入或更新某些值。 The user enters an ID and three parameters. 用户输入一个ID和三个参数。 If the ID does not exist in the database I want to return 0, fail or similiar. 如果该ID在数据库中不存在,我想返回0,则失败或类似。 When I test the code below and provide an ID that doesn't exist the stored procedure return 1 cell (Return Value) with 0 but the status gets set to 1. I guess it is because I use ToString() when I execute the query and it returns 1 cell. 当我测试下面的代码并提供一个不存在的ID时,存储过程返回1的单元格(返回值)为0,但状态设置为1。我猜这是因为我在执行查询时使用ToString()并返回1个单元格。 So how should I improve the code below? 那么我应该如何改进下面的代码?

I have this code in a method: 我在方法中有此代码:

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;

In ExecuteNonQuery I pass on the stored procedure command to the database 在ExecuteNonQuery中,我将存储过程命令传递给数据库

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

My stored procedure looks like: 我的存储过程如下所示:

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)

Thanks in advance. 提前致谢。

ExecuteNonQuery returns the number of rows affected. ExecuteNonQuery返回受影响的行数。 In this case, you're always inserting one row. 在这种情况下,您总是要插入一行。

Change your SP to read something like this: 更改您的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

Ie Only insert if the Entity Id indeed exists. 即只有在实体ID确实存在时才插入。

place this code on the top of the stored procedure 将此代码放在存储过程的顶部

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

The returned valued being displayed as "1" is the total rows returned after the execution of your query. 被显示为“ 1”的返回值是执行查询后返回的总行数。

Here is a draft of what you might need to do, this is just an idea on how the data from your procedure is being treated on the code. 这是您可能需要做的事情的草稿,这只是关于如何在代码中处理过程数据的想法。 And I am revising your sql procedure as to return the value that you are expecting in return. 而且我正在修改您的sql过程以返回您期望返回的值。

For the Code Behind: 对于后面的代码:

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();

    }
}

Your Procedure: 您的程序:

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