2011-03-24 87 views
2

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

我有这样的代码的方法:

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) 

提前致谢。

+1

尝试使用的ExecuteScalar() – KaeL 2011-03-24 06:47:55

回答

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

好返回值是0,但是当我使用状态= cmd.ExecuteNonQuery()的ToString ();它被设置为1 – Nick 2011-03-24 06:45:45

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确实存在的情况下才会插入。

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