我想从表中选择列匹配给定参数的表。如果参数为null,我想从表中选择所有记录。下面的相关代码是引发此错误的原因。

    private static string _dbJobCreate =
"CREATE TABLE Job (jID int primary key identity(1,1), jAddress nvarchar(64) not null);";

    private static string _dbJobSelect =
"SELECT jID FROM Job WHERE jAddress = @jAddress OR @jAddress IS NULL";

    public static DataTable GetJobs(string jAddress)
    {
        SqlCeParameter pjAddress = new SqlCeParameter();
        pjAddress.ParameterName = "@jAddress";

        if (!string.IsNullOrEmpty(jAddress))
        {
            pjAddress.Value = jAddress;
        }
        else
        {
            pjAddress.Value = DBNull.Value;
        }

        return ExecuteDataTable(_dbJobSelect, pjAddress);
    }


例外:The specified argument value for the function is not valid. [ Argument # = 1,Name of function(if known) = isnull ]

如何在SQLCE中有效完成此操作而不会出错?

最佳答案

您可以通过指定传递给查询的参数类型来避免此错误。因此,您需要做的是:

pjAddress.DbType = DbType.String;

10-08 16:43