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

[或许新版SqlHelper这个问题已经解决,这里给碰到同样问题的朋友一个参考]
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];
                    }

                }

            }

        }

:-)祝好运!
posted @ 2007-05-31 09:39  CalvinChu  阅读(4192)  评论(1编辑  收藏  举报