关于SqlHelper中报"Parameter count does not match Parameter Value count."错误的解决方法

本文解决了使用SQLHelper执行存储过程时,默认参数未被正确处理导致的参数数量不匹配问题。通过修改AssignParameterValues方法,使得即使未提供所有参数也能正确执行。
[或许新版SqlHelper这个问题已经解决,这里给碰到同样问题的朋友一个参考]
SQLHelper抛出的异常是说你的参数量与需要的参数不一至.但有时是默认参数,我们并不需要在程序中也提交这个参数.
如:PROCEDURE中:
ALTER PROCEDURE MYTESETPROCEDURE
(
 @Param1 int,
 @Param2 nvarchar(128) ='Default Value'
}
....
这样,其实我们提交参数一就可以执行了,但在SQLHELPER中.如果只提供一个参数会报Parameter count does not match Parameter Value count."这样的错误.

修改SQLHELPER如下:
None.gifprivate static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if ((commandParameters == null|| (parameterValues == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Do nothing if we get no data
InBlock.gif
                return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int parameterValuesLen = parameterValues.Length;
InBlock.gif            
for (int i = 0; i < parameterValuesLen; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (parameterValues[i] is IDbDataParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    IDbDataParameter paramInstance 
= (IDbDataParameter)parameterValues[i];
InBlock.gif                    
foreach (SqlParameter param in commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (param.ParameterName == paramInstance.ParameterName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if (paramInstance.Value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                param.Value 
= DBNull.Value;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                param.Value 
= paramInstance.Value;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (commandParameters.Length != parameterValues.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
throw new ArgumentException("Parameter count does not match Parameter Value count.");
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
if (parameterValues[i] == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{   
InBlock.gif                        commandParameters[i].Value 
= DBNull.Value;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        commandParameters[i].Value 
= parameterValues[i];
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

:-)祝好运!

转载于:https://www.cnblogs.com/apexchu/archive/2007/05/31/765937.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值