asp.net 标量变量误差

rqdpfwrv  于 2022-11-26  发布在  .NET
关注(0)|答案(3)|浏览(126)

请把我推出去,我有个错误
错误:
必须声明标量变量“@Criteria”。
因为我没有太多的经验,我找不出问题,谢谢
密码:

public static void CreateReveiw(string paperId, string Criteria_Id, string Grate)
{


    var sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);



    SqlCommand cmd = new SqlCommand();
    cmd.Connection = sqlCon;
    cmd.CommandText = "select Review_paper_ID from ReviewPaper where Paper_ID= @PaperId";
    cmd.Parameters.AddWithValue("@PaperId", paperId);
    cmd.Connection.Open();
    string Review_paper_ID = cmd.ExecuteScalar().ToString();
    cmd.Connection.Close();
    cmd.Dispose();



    string query2 = @"insert into ReviewPaperCriteria(Review_paper_ID,Criteria_ID,Grate) 
                     values(@Review_paper_ID,@Criteria,@Grate);select SCOPE_IDENTITY() as RPID";

    cmd = new SqlCommand(query2, sqlCon);
    cmd.Parameters.AddWithValue("@Review_paper_ID", Review_paper_ID);
    cmd.Parameters.AddWithValue("@Criteria_ID", Criteria_Id);
    cmd.Parameters.AddWithValue("@Grate", Grate);

    string ReviewPaperId;

    try
    {
        cmd.Connection.Open();
        ReviewPaperId = cmd.ExecuteScalar().ToString();
        cmd.Connection.Close();
    }
    catch (Exception ee) { throw ee; }
    finally { cmd.Dispose(); }


    }
ppcbkaq5

ppcbkaq51#

变更

cmd.Parameters.AddWithValue("@Criteria_ID", Criteria_Id);

cmd.Parameters.AddWithValue("@Criteria", Criteria_Id);

或者更改查询字符串(query 2)以匹配所使用的参数名称(@Criteria_ID)。

im9ewurl

im9ewurl2#

您的参数是@Criteria_ID,但您在SQL中引用了@Criteria。您需要在其中一个位置更改该参数以保持一致。

string query2 = @"insert into ReviewPaperCriteria(Review_paper_ID,Criteria_ID,Grate) 
                 values(@Review_paper_ID,@Criteria,@Grate);select SCOPE_IDENTITY() as RPID";
                                         ^^^^^^^^^    
...

cmd.Parameters.AddWithValue("@Criteria_ID", Criteria_Id);
                             ^^^^^^^^^^^^
b4qexyjb

b4qexyjb3#

您正在声明变量@Criteria,但试图使用@Criteria_ID设置其值。
变更

cmd.Parameters.AddWithValue("@Criteria_ID", Criteria_Id);

结束日期

cmd.Parameters.AddWithValue("@Criteria", Criteria_Id);

相关问题