【发布时间】:2016-10-25 12:58:31
【问题描述】:
我正在尝试在 c# 中执行存储过程。
存储过程执行插入操作并返回其中一列的值作为输出参数。
我需要从 c# 中执行它并取回输出参数的值。
以下是我目前尝试过的。
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());
上面执行,插入成功,但输出参数值返回null。
谁能建议我在这里缺少什么。
谢谢
【问题讨论】:
-
需要声明接收返回值的参数,并将其方向设置为返回值。在此处检查解决方案:stackoverflow.com/questions/706361/…
-
我已经这样做了,有问题更新了。
-
这样试试。 associateParams[9] = new SqlParameter("@activationKey", SqlDbType.Int); associateParams[9].Value = -1; associateParams[9].Direction = ParameterDirection.Output;
-
存储过程中是否设置了参数的值? SSMS调用SP时参数有值吗?
-
@Dirk。是的,我正在设置值......但即使从工作室运行,它也没有返回任何东西......
标签: c# .net sql-server ado.net