我正在使用System.Data.OracleClient,它按名称进行参数绑定,并验证CommandText和Parameters是否同步:
public string CommandText { get; set; } public IEnumerable<OracleParameter> Parameters { get; set; } private void VerifyThatAllParametersAreBound() { var variableNames = Regex.Matches(CommandText,":\\w+") .Cast<Match>().Select(m => m.Value).ToArray(); var parameteterNames = Parameters.Select(p => p.ParameterName).ToArray(); var unboundVariables = variableNames.Except(parameteterNames).ToArray(); if (unboundVariables.Length > 0) { throw new Exception("Variable in CommandText missing parameter: " + string.Join(",",unboundVariables) + "."); } var unboundParameters = parameteterNames.Except(variableNames).ToArray(); if (unboundParameters.Length > 0) { throw new Exception("Parameter that is not used in CommandText: " + string.Join(",unboundParameters) + "."); } }
还有一个查询抛出ORA-01008:并非所有变量都绑定.当手动将参数值插入有问题的CommandText时,查询运行,因此CommandText和Parameters-values应该没问题.我正在使用:作为变量和参数名称的前缀,它适用于其他查询.
如何查明此异常的原因?