标量变量错误

时间:2011-09-01 14:51:02

标签: asp.net sql

如果你请我把我弄错,我有一个错误

错误:

必须声明标量变量“@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(); }



    }

3 个答案:

答案 0 :(得分:3)

更改

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

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

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

答案 1 :(得分:3)

您的参数是@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);
                             ^^^^^^^^^^^^

答案 2 :(得分:1)

您要声明变量@Criteria,但尝试使用@Criteria_ID设置其值。

更改

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

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