繁体   English   中英

声明标量变量C#

[英]Declare scalar variable c#

执行代码时出现以下错误:

标量变量“ @qte”必须声明。

        Connection newconnection = new Connection();
        newconnection.Connection_Today();

        SqlCommand comm = new SqlCommand();
        comm.Connection = Connection.conn;

        comm.CommandText = "UPDATE F_DOCLIGNE SET DO_DateLivr = @date, DL_Qte = @qte, DL_Remise01REM_Valeur = @remise, DL_PrixUnitaire = @pu, DL_MontantHT = (@pu - ((@remise * @pu) / 100)) * @qte where AR_Ref = @code";

        SqlParameter param = new SqlParameter("qte", SqlDbType.Int);
        SqlParameter param1 = new SqlParameter("remise", SqlDbType.Int);

        comm.Parameters.AddWithValue("@date", textBox_livr.Text);
        comm.Parameters.Add("@pu", SqlDbType.Int).Value = textBox_prix.Text;
        comm.Parameters.Add("@code", SqlDbType.VarChar).Value = textBox_art.Text;

        comm.ExecuteNonQuery();

当我尝试comm.Parameters.AddWithValue("@qte", DL_Qte)出现错误

名称“ DL_Qte”在当前上下文中不存在

我的变量@qte和@remise具有数据库中字段的值,它们用于计算金额。

如何在不执行程序的情况下声明标量变量?

您使用以下命令创建了参数“ @qte”(变量“ param”)的实例

SqlParameter param = new SqlParameter("qte", SqlDbType.Int);

但未在comm.Parameters.Add中添加此“参数”,所以这就是缺少此参数的原因。

所以你应该这样做:

comm.Parameters.Add(param);

例如,如果@qte是已经来自数据库的值,则它不是变量。 您可以使用变量将代码中的值发送到数据库。 以您的情况(如果我理解正确),您需要重写更新语句:

UPDATE tablename SET column = (select 1) where otherecolum = @var

在离线情况下,您必须提供“选择1”实现以查询正确的值。

如您所暗示的那样,如果您想使用DL_Qte和DL_Remise01REM_Valeur字段的值而不是更新它们-如果它们在您要更新的同一行中,则可以直接使用它们:

comm.CommandText = @"UPDATE F_DOCLIGNE SET DO_DateLivr = @date, DL_PrixUnitaire = @pu, 
        DL_MontantHT = (@pu - ((DL_Remise01REM_Valeur * @pu) / 100)) * DL_Qte 
        where AR_Ref = @code";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM