SQLHelper抛出的异常是说你的参数量与需要的参数不一至.但有时是默认参数,我们并不需要在程序中也提交这个参数.
如:PROCEDURE中:
ALTER PROCEDURE MYTESETPROCEDURE
(
@Param1 int,
@Param2 nvarchar(128) ='Default Value'
}
....
这样,其实我们提交参数一就可以执行了,但在SQLHELPER中.如果只提供一个参数会报Parameter count does not match Parameter Value count."这样的错误.
修改SQLHELPER如下:
private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
{
if ((commandParameters == null) || (parameterValues == null))
{
// Do nothing if we get no data
return;
}
int parameterValuesLen = parameterValues.Length;
for (int i = 0; i < parameterValuesLen; i++)
{
if (parameterValues[i] is IDbDataParameter)
{
IDbDataParameter paramInstance = (IDbDataParameter)parameterValues[i];
foreach (SqlParameter param in commandParameters)
{
if (param.ParameterName == paramInstance.ParameterName)
{
if (paramInstance.Value == null)
{
param.Value = DBNull.Value;
}
else
{
param.Value = paramInstance.Value;
}
break;
}
}
}
else
{
if (commandParameters.Length != parameterValues.Length)
{
throw new ArgumentException("Parameter count does not match Parameter Value count.");
}
if (parameterValues[i] == null)
{
commandParameters[i].Value = DBNull.Value;
}
else
{
commandParameters[i].Value = parameterValues[i];
}
}
}
}
{
if ((commandParameters == null) || (parameterValues == null))
{
// Do nothing if we get no data
return;
}
int parameterValuesLen = parameterValues.Length;
for (int i = 0; i < parameterValuesLen; i++)
{
if (parameterValues[i] is IDbDataParameter)
{
IDbDataParameter paramInstance = (IDbDataParameter)parameterValues[i];
foreach (SqlParameter param in commandParameters)
{
if (param.ParameterName == paramInstance.ParameterName)
{
if (paramInstance.Value == null)
{
param.Value = DBNull.Value;
}
else
{
param.Value = paramInstance.Value;
}
break;
}
}
}
else
{
if (commandParameters.Length != parameterValues.Length)
{
throw new ArgumentException("Parameter count does not match Parameter Value count.");
}
if (parameterValues[i] == null)
{
commandParameters[i].Value = DBNull.Value;
}
else
{
commandParameters[i].Value = parameterValues[i];
}
}
}
}
:-)祝好运!