EF中执行存储过程报错 String[4]: Size 属性具有无效大小值 0
排查后是如下问题所致,给定的参数没有设定大小(加入红框内的就可以了)
private string GetCode(MCPERPEntities db, string code)
{ List<SqlParameter> paramArray = new List<SqlParameter>();
paramArray.Add(new SqlParameter("@I_CodeName", code));
paramArray.Add(new SqlParameter("@I_Year", DateTime.Now.ToString("yyyy")));
paramArray.Add(new SqlParameter("@I_Month", DateTime.Now.ToString("MM")));
paramArray.Add(new SqlParameter("@I_Day", DateTime.Now.ToString("dd")));
SqlParameter param = new SqlParameter("@O_Code", SqlDbType.NVarChar);
param.Direction = ParameterDirection.Output;
paramArray.Add(param);
paramArray[0].SqlDbType = SqlDbType.VarChar;
paramArray[0].Size = 200;
paramArray[1].SqlDbType = SqlDbType.VarChar;
paramArray[1].Size = 4;
paramArray[2].SqlDbType = SqlDbType.VarChar;
paramArray[2].Size = 2;
paramArray[3].SqlDbType = SqlDbType.VarChar;
paramArray[3].Size = 2; try
{
db.Database.ExecuteSqlCommand("EXEC P_Sys_GetAutoCode @I_CodeName,@I_Year,@I_Month,@I_Day,@O_Code out", paramArray.ToArray());
}
catch (Exception ex)
{
throw;
}
return paramArray[3].Value.ToString();
}
写的比较粗糙因为是一个简单的测试就没有写的很细致.希望能帮到遇到这个问题的同学.