Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Prepare方法要求所有参数都具有显式设置的类型_C#_Tsql_Prepared Statement_Sqlcommand - Fatal编程技术网

C# Prepare方法要求所有参数都具有显式设置的类型

C# Prepare方法要求所有参数都具有显式设置的类型,c#,tsql,prepared-statement,sqlcommand,C#,Tsql,Prepared Statement,Sqlcommand,我的WCF web服务中有以下代码片段,它根据提供的字典值的格式构建了一组where条件 public static Dictionary<string, string>[] VehicleSearch(Dictionary<string, string> searchParams, int maxResults) { string condition = ""; foreach (string key in searchParams.Keys)

我的WCF web服务中有以下代码片段,它根据提供的字典值的格式构建了一组where条件

public static Dictionary<string, string>[] VehicleSearch(Dictionary<string, string> searchParams, int maxResults)
{
    string condition = "";
    foreach (string key in searchParams.Keys)
    {
        //Split out the conditional in case multiple options have been set (i.e. SUB;OLDS;TOY)
        string[] parameters = searchParams[key].Split(';');
        if (parameters.Length == 1)
        {
            //If a single condition (no choice list) check for wildcards and use a LIKE if necessary
            string predicate = parameters[0].Contains('%') ? " AND {0} LIKE @{0}" : " AND {0} = @{0}";
            condition += String.Format(predicate, key);
        }
        else
        {
            //If a choice list, split out query into an IN condition
            condition += string.Format(" AND {0} IN({1})", key, string.Join(", ", parameters));
        }
    }

    SqlCommand cmd = new SqlCommand(String.Format(VEHICLE_QUERY, maxResults, condition));
    foreach (string key in searchParams.Keys)
        cmd.Parameters.AddWithValue("@" + key, searchParams[key]);
    cmd.Prepare();
我说的没错

System.InvalidOperationException:SqlCommand.Prepare方法需要 所有参数都必须具有显式设置的类型

我所做的所有搜索都表明,我需要告诉
AddWithValue
我正在准备的值的类型,但我准备的所有值都是字符串,我看到的所有示例在处理字符串时都不会执行任何额外的操作。我遗漏了什么?

而不是这个:

cmd.Parameters.AddWithValue("@" + key, searchParams[key]);
您需要使用以下内容:

SELECT TOP 200 MVINumber AS MVINumber
    , LicensePlateNumber
    , VIN
    , VehicleYear
    , MakeG
    , ModelG
    , Color
    , Color2
FROM [Vehicle_Description_v]
WHERE 1=1 AND VIN LIKE @VIN
cmd.Parameters.Add("@" + key, SqlDbType.******).Value = searchParams[key];
您需要能够以某种方式确定您的参数必须是什么数据类型

这可能类似于:

  • 整数值的
    SqlDbType.Int
  • SqlDbType.VarChar
    用于非Unicode字符串(不要忘记指定字符串的长度)
  • 用于guid的SqlDbType.UniqueIdentifier
    等等
使用
AddWithValue
是很方便的,但是它让ADO.NET根据传入的值猜测数据类型。大多数时候,这些猜测都是相当不错的——但有时,它们可能是错的


我建议您始终明确地说出所需的数据类型

如果您阅读了,您将看到当您使用SQLCommand.Prepare时,您需要使用参数。为每个参数添加并分配一个数据类型。该链接中有一个很好的代码示例,它将向您展示如何操作。

为什么
1=1
?考虑到它总是正确的,并且您正在使用
,它将始终取决于
如@vin
,这意味着
1=1
完全是不必要的。Elviejo,我有时在某处代码中构建动态查询时使用类似于WHERE 1=1的东西,因此我的其余行是“and something”而不是试图找出哪一行是第一行,并把一个地方只是为该行和其余的行。EIVieejo,是的,正是吉姆说的。我在语句的开头使用1=1,因此我所要做的就是为我的每个条件附加“AND”。因为1=1不会真正应用,所以使用它可以保持代码非常干净。嘿,我也做了同样的“where 1=1”的事情。这对一些小的更改起到了作用。使用SqlDbType代替该值会产生相同的错误,我认为这是因为它在该参数中需要一个值。相反,我将其分配给prep变量并添加了
prep.SqlDbType=SqlDbType.VarChar,然后为prep指定了一个大小。要扩展Taidani的答案,您可以这样做:
cmd.Parameters.Add(“@PARAM”,SqlDbType.TYPE,VALUE.Length)。VALUE=VALUE
我相信你的意思是
Add
而不是第二个
AddWithValue
@marc\s。否则。@GSerg:哦,是的,我做到了!:-)谢谢-修正了。