Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL存储过程不返回字符串值_C#_Sql_Asp.net_Sql Server_Stored Procedures - Fatal编程技术网

C# SQL存储过程不返回字符串值

C# SQL存储过程不返回字符串值,c#,sql,asp.net,sql-server,stored-procedures,C#,Sql,Asp.net,Sql Server,Stored Procedures,我有以下存储过程的代码。我只想返回消息,如果已经存在于数据库中,它应该返回消息,否则将重复插入数据并显示消息完成 SQL存储过程 无法返回显示以下错误的字符串值: Msg 245, Level 16, State 1, Procedure SP_AddProjectManager, Line 43 [Batch Start Line 2] Conversion failed when converting the varchar value 'Repeated' to data type int

我有以下存储过程的代码。我只想返回消息,如果已经存在于数据库中,它应该返回消息,否则将重复插入数据并显示消息完成

SQL存储过程

无法返回显示以下错误的字符串值:

Msg 245, Level 16, State 1, Procedure SP_AddProjectManager, Line 43 [Batch Start Line 2]
Conversion failed when converting the varchar value 'Repeated' to data type int.
C代码

使用param,如以下……免责声明,不要在sql编辑器前使用

ALTER PROCEDURE [dbo].[SP_AddProjectManager]
        -- Add the parameters for the stored procedure here
        @ProjectId int,
        @EmployeeId int,
        @uid nvarchar(128),
        @output nvarchar(50) OUTPUT
        
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
        declare @Organization_Id int = (select Organization_Id from Employees where User_Account_Id = @uid);
        declare @Office_Id int = (select Office_Id from Employees where User_Account_Id = @uid);
        declare @output nvarchar(50);
        declare @dup int =(select count(*) from Project_Managers where EmployeeId=@EmployeeId and ProjectId=@ProjectId);
        
        
    
        if(@dup=0)
            begin
                INSERT INTO [dbo].[Project_Managers]
                       ([EmployeeId]
                       ,[ProjectId]
                       ,[User_Account_Id]
                       ,[Office_Id]
                       ,[Organization_Id]
                       ,[CreatedOn]
                       ,[UpdatedOn]
                       ,[CreatedBy]
                       ,[UpdatedBy]
                       ,[Is_active]
                       )
                 VALUES(@EmployeeId,@ProjectId,@uid,@Office_Id,@Organization_Id,GETDATE(),GETDATE(),@uid,@uid,1);
                 SET @output= 'Done';
             end
         else
             begin
                SET @output ='Repeated';
             end
       
    END
你可以这样称呼它

DECLARE @output varchar(50);
EXEC [dbo].[SP_AddProjectManager] 6, 66, '81873272', @output OUTPUT
SELECT @output 
-- OR
PRINT @Output
或者,如果不需要输出参数,请用SELECT替换RETURN语句

你的C代码,再一次,没有使用编辑器,所以在我的脑海里

public static string Add_ProjectManager(ProjectManagers PM)
{
    using (SqlCommand cmd_insertion = new SqlCommand())
    {
        using (conn c = new conn()) // Whatever this is
        {
            using (SqlConnection _Con = c.conect())
            {
                cmd_insertion.Connection = _Con;
                cmd_insertion.CommandType = System.Data.CommandType.StoredProcedure;
                cmd_insertion.CommandText = "SP_AddProjectManager";
                cmd_insertion.Parameters.AddWithValue("@ProjectId", PM.ProjectId); // .AddWithValue() is bad
                cmd_insertion.Parameters.AddWithValue("@EmployeeId", PM.EmployeeId);
                cmd_insertion.Parameters.AddWithValue("@uid", PM.userId);

                SqlParameter retval = cmd_insertion.Parameters.Add("@output", SqlDbType.VarChar);
                retval.Direction = ParameterDirection.Output;

                _Con.Open();

                cmd_insertion.ExecuteNonQuery();
                string status = retval.Value;
                _Con.Close();
                return status;
            }
        }
    }
}

您将@output声明为int,而您的意思可能是nvarchar。。。。投票以键入错误的形式关闭此项。这是一个存储过程,它只能返回int…您可以选择结果或传入输出参数并进行设置。即使使用nvarchar,它也会显示相同的错误亲爱的所有,我已经更新了代码,它也有相同的错误如果你想,你可以使用printoutput,并在messages选项卡中查看我是否应该添加相同的存储过程我很抱歉我不理解请详细说明第一个代码示例是你的存储过程,带有一个输出参数。第二个示例是如何从SQL中调用它。您如何使用存储过程?结果用于什么?我想调用asp.net代码SqlParameter retval=cmd_insertion.Parameters。Add@output,SqlDbType.VarChar;retval.Direction=参数Direction.ReturnValue;啊……你在帖子里没有提到C。因此,将您的ParameterDirection.ReturnValue替换为ParameterDirection.Output…在代码中执行存储过程,然后您可以访问retval.Value,或者,在您的问题中发布您的C代码,我会更正它。请检查编辑的答案@amaddurrani
DECLARE @output varchar(50);
EXEC [dbo].[SP_AddProjectManager] 6, 66, '81873272', @output OUTPUT
SELECT @output 
-- OR
PRINT @Output
public static string Add_ProjectManager(ProjectManagers PM)
{
    using (SqlCommand cmd_insertion = new SqlCommand())
    {
        using (conn c = new conn()) // Whatever this is
        {
            using (SqlConnection _Con = c.conect())
            {
                cmd_insertion.Connection = _Con;
                cmd_insertion.CommandType = System.Data.CommandType.StoredProcedure;
                cmd_insertion.CommandText = "SP_AddProjectManager";
                cmd_insertion.Parameters.AddWithValue("@ProjectId", PM.ProjectId); // .AddWithValue() is bad
                cmd_insertion.Parameters.AddWithValue("@EmployeeId", PM.EmployeeId);
                cmd_insertion.Parameters.AddWithValue("@uid", PM.userId);

                SqlParameter retval = cmd_insertion.Parameters.Add("@output", SqlDbType.VarChar);
                retval.Direction = ParameterDirection.Output;

                _Con.Open();

                cmd_insertion.ExecuteNonQuery();
                string status = retval.Value;
                _Con.Close();
                return status;
            }
        }
    }
}