gpt4 book ai didi

c# - 参数 C# SQL 出错

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:34 29 4
gpt4 key购买 nike

我需要验证参数 RGP, PEIXE, DATA_REGISTRO 让我的方法返回 true。但是在 PEIXE 参数上给出错误:

Expects 'PEIXE' parameter, which was not provided.

PEIXE是SQL上的Varchar类型,RGP是Int类型,DATA_REGISTRO是Date类型。

 public bool Search_RGP_Cadastro(int param_RGP, string param_date, string param_peixe)
{
SqlDataReader objReader;
SqlCommand objcmd = null;

vsql = "SELECT [RGP], [PEIXE], [PESO], [QUANTIDADE], [DATA_REGISTRO] FROM cadastro WHERE RGP = @RGP and PEIXE = @PEIXE and DATA_REGISTRO = @DATA_REGISTRO";

if (this.Conectar())
{
try
{
DateTime dtParam = DateTime.Parse(param_date);

objcmd = new SqlCommand(vsql, objCon);

objcmd.Parameters.Add(new SqlParameter("@RGP", param_RGP));
objcmd.Parameters.Add(new SqlParameter("@PEIXE", param_peixe));
objcmd.Parameters.Add(new SqlParameter("@DATA_REGISTRO", dtParam));

objReader = objcmd.ExecuteReader();

if (objReader.Read())
{
valor.retorna_RGP = objReader.GetInt32(0);
valor.retorna_nome_peixe = objReader.GetString(1);
valor.retorna_peso = objReader.GetDouble(2);
valor.retorna_Quantidade = objReader.GetInt32(3);
valor.retorna_date_time = objReader.GetDateTime(4);
}
return true;
}
catch
{
throw;
}
finally
{
this.Desconectar();
}


}
else
return false;


}

最佳答案

我会用单引号将您的 param_peixe 括起来,如下所示:

objcmd.Parameters.Add(new SqlParameter("@PEIXE", "'" + param_peixe + "'"));

这不是我第一次看到 SQL 拒绝将字符串用作 VARCHAR。

关于c# - 参数 C# SQL 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31569278/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com