0

我有一个带有这些参数的 SQL 过程

@WorkflowApprovalHistoryId      int                 OUTPUT,
    @ProjectUid                     uniqueidentifier,
    @ProjectId                      nvarchar(50),
    @StageUid                       uniqueidentifier,
    @Username                       nvarchar(50),
    @WasRejected                    bit,
    @Cost                           money,
    @Work                           money,
    @StartDate                      datetime,
    @FinishDate                     datetime,
    @InsertDate                     datetime            OUTPUT

我已经检查了这个Failed To Convert Parameter Value From A String To A Int32,但它对我没有帮助,因为我没有任何输入参数作为 int。

我在这里粘贴我的代码

private void LogApproval(Guid prjUId, bool wasRejected) {
            string projectId = string.Empty;
            Nullable<Guid> stageUid = null;
            Nullable<decimal> cost = null;
            Nullable<decimal> work = null;
            Nullable<DateTime> startDate = null;
            Nullable<DateTime> finishDate = null;
            string stageGuid = string.Empty;
            //Processing here
            SqlConnection conn = null;
            conn = new SqlConnection(getConnectionString());
            conn.Open();
            SqlCommand cmnd=new SqlCommand();
            cmnd.CommandText="ProcedureName";
            cmnd.CommandType=CommandType.StoredProcedure;
            cmnd.Connection=conn;
            SqlParameter param1=new SqlParameter();
            param1=cmnd.Parameters.Add("@WorkflowApprovalHistoryId",SqlDbType.Int);
            param1.Direction=ParameterDirection.ReturnValue;

            SqlParameter param2=new SqlParameter();
            param2=cmnd.Parameters.Add("@ProjectUid",SqlDbType.UniqueIdentifier);//sharepoint
            param2.Direction=ParameterDirection.Input;
            param2.Value = System.Data.SqlTypes.SqlGuid.Parse(prjUId.ToString());
            //param2.Value=prjUId;
            SqlParameter param3=new SqlParameter();
            param3=cmnd.Parameters.Add("@ProjectId",SqlDbType.NVarChar);
            param3.Direction=ParameterDirection.Input;
            param3.Value=projectId;
            SqlParameter param4=new SqlParameter();
            param4=cmnd.Parameters.Add("@StageUid",SqlDbType.UniqueIdentifier);// PSI
            param4.Direction=ParameterDirection.Input;
            param4.Value = System.Data.SqlTypes.SqlGuid.Parse(stageUid.ToString());
            //param4.Value=stageUid;
            SqlParameter param5=new SqlParameter();
            param5=cmnd.Parameters.Add("@Username",SqlDbType.NVarChar); //unidentified
            param5.Direction=ParameterDirection.Input;
            param1.Value="userName";
            SqlParameter param6=new SqlParameter();
            param6=cmnd.Parameters.Add("@WasRejected",SqlDbType.Bit);//form status provide as input
            param6.Direction=ParameterDirection.Input;
            if (wasRejected == true)
                param6.Value = 1;
            else
                param6.Value = 0;
            SqlParameter param7=new SqlParameter();
            param7=cmnd.Parameters.Add("@Cost",SqlDbType.Money); //PSI
            param7.Direction=ParameterDirection.Input;
            param7.Value=cost;
            SqlParameter param8=new SqlParameter();
            param8=cmnd.Parameters.Add("@Work",SqlDbType.Money);//PSI
            param8.Direction=ParameterDirection.Input;
            param8.Value=work;
            SqlParameter param9=new SqlParameter();
            param9=cmnd.Parameters.Add("@StartDate",SqlDbType.DateTime);// PSI
            param9.Direction=ParameterDirection.Input;
            param9.Value=startDate;
            SqlParameter param10=new SqlParameter();
            param10=cmnd.Parameters.Add("@FinishDate",SqlDbType.DateTime);// PSI
            param10.Direction=ParameterDirection.Input;
            param10.Value=finishDate;


            SqlParameter param11=new SqlParameter();
            param11=cmnd.Parameters.Add("@InsertDate",SqlDbType.DateTime);
            param11.Direction = ParameterDirection.ReturnValue;

            cmnd.ExecuteNonQuery();
            var HistoryID = param1.Value;
            var InsertDate = param11.Value;
}

在执行 Execute NonQuery 时抛出 Failed to convert parameter value from a String to a Int32

4

1 回答 1

2

输出参数应该是 not ParameterDirection.Output, not ParameterDirection.ReturnValue

ParameterDirection.ReturnValuereturn用于 SQL语句生成的存储过程的整数结果值

顺便说一下,Parameters.AddWithValue建议超过Parameters.Add输入值

于 2012-06-19T10:39:47.917 回答