简体   繁体   English

使用ExecuteScalar()执行SP时如何在c#中获取输出参数的值

[英]How to get values of output parameters in c# while executing SP using ExecuteScalar()

I am trying to execute a stored procedure in c#. 我正在尝试在C#中执行存储过程。

Stored procedure performs insert operation and returns values of one of the columns as output parameter. 存储过程执行插入操作,并返回列之一的值作为输出参数。

I need to execute it from c# and get back value of output param. 我需要从c#中执行它并获取输出参数的值。

Below is what i have tried so far. 以下是到目前为止我尝试过的。

SqlParameter[] associateParams = new SqlParameter[10];
            {
                 associateParams[0]=new SqlParameter("@orgName", newAssociate.OrgName);
                 associateParams[1]=new SqlParameter("@createdBy", newAssociate.Email);
                 associateParams[2]=new SqlParameter("@userName", newAssociate.UserName);
                 associateParams[3]=new SqlParameter("@workEmail", newAssociate.Email);
                 associateParams[4]=new SqlParameter("@password", newAssociate.Password);
                 associateParams[5]=new SqlParameter("@teamStrength", "0");
                 associateParams[6]=new SqlParameter("@projName", newAssociate.ProjName);
                 associateParams[7]=new SqlParameter("@userType", "Associate");
                 associateParams[8] = new SqlParameter("@userSalt", SqlDbType.VarChar, 400);
                 associateParams[8].Direction = ParameterDirection.Output;
                 associateParams[9] = new SqlParameter("@activationKey", SqlDbType.Int);
                 associateParams[9].Direction = ParameterDirection.Output;

            }

        using (SqlCommand cmd = con.CreateCommand())
        {
            log.Debug("In command is called");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = ProcedureName;
            cmd.Parameters.AddRange(param);

            log.Debug("Command is called");
            try
            {
                if (con.State != ConnectionState.Open)
                {
                    con.Open();

                    log.Debug("Con is open");
                }
                cmd.ExecuteScalar();
                log.Debug(cmd.Parameters["@userSalt"].Value.ToString());
                log.Debug(cmd.Parameters["@activationKey"].Value.ToString());

Executing above, performs insert successfully but returns null for output params values. 执行上面的操作,成功执行插入,但是对于输出参数值返回null。

Can anyone suggest what I am missing here. 谁能建议我在这里所缺少的。

Thanks 谢谢

try like this when you define output parameters: 定义输出参数时,请尝试以下操作:

         associateParams[8] = new SqlParameter("@userSalt", SqlDbType.VarChar, 400);
         associateParams[8].Value = "";
         associateParams[8].Direction = ParameterDirection.Output;
         associateParams[9] = new SqlParameter("@activationKey", SqlDbType.Int);                 
         associateParams[9].Value = 0;
         associateParams[9].Direction = ParameterDirection.Output;

let me know if this helps. 让我知道这是否有帮助。

UPDATE: here is my own method 更新:这是我自己的方法

 cmd.Parameters.Add(new SqlParameter("@userSalt", SqlDbType.VarChar, 400));
 cmd.Parameters["@userSalt"].Value = "";
 cmd.Parameters["@userSalt"].Direction = ParameterDirection.Output;

UPDATE1: because you dont use ExecuteNonQuery. UPDATE1:因为您不使用ExecuteNonQuery。 Change cmd.ExecuteScalar() to cmd.ExecuteNonQuery() cmd.ExecuteScalar()更改为cmd.ExecuteNonQuery()

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

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