0

我正在尝试从数据库中填充列表。这是我的代码:

    string cur = dInstructorSelect.SelectedValue.Substring(dInstructorSelect.SelectedValue.IndexOf(" - ") + 3);
    SqlCommand cmdInsCourses = new SqlCommand("select * from CourseTable where InstructorID=@cur", con);
    cmd.Parameters.AddWithValue("@cur", cur);
    SqlDataAdapter da = new SqlDataAdapter(cmdInsCourses);
    DataTable dt = new DataTable();
    da.Fill(dt);

在这里,我声明了一个字符串变量 cur,它按预期返回 4。问题是,当我调试时,有一个错误说必须声明标量变量“@cur”。我看不出这里有什么问题。任何人都可以帮忙吗?

谢谢

编辑:这是完整的代码:

SqlConnection con = new SqlConnection();
    con.ConnectionString = Userfunctions.GetConnectionString();


    int result;

    string queryCourseCount = "select count (*) from CourseTable";


    SqlCommand countCommand = new SqlCommand(queryCourseCount, con);
    con.Open();
    int courseCount = 1001 + Convert.ToInt32(countCommand.ExecuteScalar());
    string crn = (MyGlobals.currentYear % 100).ToString() + (MyGlobals.currentTerm == "Spring" ? 2 : 1) + courseCount.ToString().Substring(1, 3);

    string instructor = dInstructorSelect.SelectedValue.Substring(dInstructorSelect.SelectedValue.IndexOf(" - ")+3);
    string subject = dSubject.SelectedValue, courseNumber=tCourse.Text, courseName= tCourseName.Text ;
    string courseDescription = tCourseDescription.Text, capacity=tCapacity.Text;
    string currentTerm=MyGlobals.currentTerm + " " + MyGlobals.currentYear.ToString();
    string level=dLevel.SelectedValue, credit=tCredit.Text;
    string query1 = "insert into CourseTable(InstructorID,CourseCode,CourseNumber,CourseName,Term, CRN,Level,Credit,Description,Capacity) values(@instructor,@subject,@courseNumber,@courseName,@currentTerm,@crn,@level,@credit,@courseDescription,@capacity)";

    SqlCommand cmd = new SqlCommand(query1, con);
    cmd.Parameters.AddWithValue("@instructor", instructor);
    cmd.Parameters.AddWithValue("@subject", subject);
    cmd.Parameters.AddWithValue("@courseNumber", courseNumber);
    cmd.Parameters.AddWithValue("@courseName", courseName);
    cmd.Parameters.AddWithValue("@currentTerm", currentTerm);
    cmd.Parameters.AddWithValue("@crn", crn);
    cmd.Parameters.AddWithValue("@level", level);
    cmd.Parameters.AddWithValue("@credit", credit);
    cmd.Parameters.AddWithValue("@courseDescription", courseDescription);
    cmd.Parameters.AddWithValue("@capacity", capacity);






    string query2 = "";
    string query3 = "";
    if (cbPreq1.Checked)
    {
        query2 = "insert into PrereqTable(CourseCode,CourseNumber,Term,pCourseCode,pCourseNumber) values ('"
            + dSubject.SelectedValue + "'" + "," + "'" + tCourse.Text + "'" + "," + "'" + MyGlobals.currentTerm + " " + MyGlobals.currentYear.ToString()
            + "'" + "," + "'" + dPrereq1.SelectedValue.Substring(0, dPrereq1.SelectedValue.Length - 3) + "'" + "," + "'" + dPrereq1.SelectedValue.Substring(dPrereq1.SelectedValue.Length - 3, 3) + "'" + ")";
    }
    if (cbPreq2.Checked)
    {
       query3 = "insert into PrereqTable(CourseCode,CourseNumber,Term,pCourseCode,pCourseNumber) values ('"
            + dSubject.SelectedValue + "'" + "," + "'" + tCourse.Text + "'" + "," + "'" + MyGlobals.currentTerm + " " + MyGlobals.currentYear.ToString()
            + "'" + "," + "'" + dPrereq2.SelectedValue.Substring(0, dPrereq2.SelectedValue.Length - 3) + "'" + "," + "'" + dPrereq2.SelectedValue.Substring(dPrereq2.SelectedValue.Length - 3, 3) + "'" + ")";
    }

    string query4="";
    if (cbtime1.Checked)
    {
        query4 = "insert into TimeTable(CourseCode, CourseNumber, Term, StartHour, EndHour, Day) values ('"
            + dSubject.SelectedValue + "'" + "," + "'" + tCourse.Text + "'" + "," + "'" + MyGlobals.currentTerm + " " + MyGlobals.currentYear.ToString()
            + "'" + "," + "'" + dHourStart.SelectedValue + "'" + "," + "'" + dHourEnd.SelectedValue + "'" + "," + "'" + dDay.SelectedValue + "'" + ")";
    }
    string query5 = "";
    if (cbtime2.Checked)
    {
        query5 = "insert into TimeTable(CourseCode, CourseNumber, Term, StartHour, EndHour, Day) values ('"
          + dSubject.SelectedValue + "'" + "," + "'" + tCourse.Text + "'" + "," + "'" + MyGlobals.currentTerm + " " + MyGlobals.currentYear.ToString()
          + "'" + "," + "'" + dHourStart2.SelectedValue + "'" + "," + "'" + dHourEnd2.SelectedValue + "'" + "," + "'" + dDay2.SelectedValue + "'" + ")";
    }
    string query6="";

    if (cbtime3.Checked)
    {
        query6 = "insert into TimeTable(CourseCode, CourseNumber, Term, StartHour, EndHour, Day) values ('"
           + dSubject.SelectedValue + "'" + "," + "'" + tCourse.Text + "'" + "," + "'" + MyGlobals.currentTerm + " " + MyGlobals.currentYear.ToString()
           + "'" + "," + "'" + dHourStart3.SelectedValue + "'" + "," + "'" + dHourEnd3.SelectedValue + "'" + "," + "'" + dDay3.SelectedValue + "'" + ")";
    }

    SqlCommand cmd1, cmd2, cmd3, cmd4, cmd5, cmd6;

    bool correctTime = false;
    List<String> timeTable = new List<string>();

    List<Course>instCourses = new List<Course>();
    string tableName = "InstructorTable";
  //  String name = "", surname = "", email = "";
  //  CreateUser(con, tableName, ref name, ref surname, ref email);

  //  MyGlobals.instructor = new Instructor(Convert.ToInt32(idBox.Text), "Active", email, name, surname, passwordBox.Text);

    string cur = dInstructorSelect.SelectedValue.Substring(dInstructorSelect.SelectedValue.IndexOf(" - ") + 3);
    SqlCommand cmdInsCourses = new SqlCommand("select * from CourseTable where InstructorID=@cur", con);
    cmd.Parameters.AddWithValue("@cur", cur);
    SqlDataAdapter da = new SqlDataAdapter(cmdInsCourses);
    DataTable dt = new DataTable();
    da.Fill(dt);

注意:我知道有些查询没有参数化,我会修复它们。

4

3 回答 3

1

尝试为您的参数添加前缀@

cmdInsCourses.Parameters.AddWithValue("@cur", cur);
于 2013-06-05T10:44:24.850 回答
1

尝试添加标量变量“@cur”而不是“cur”:

 cmd.Parameters.AddWithValue("@cur", cur);
于 2013-06-05T10:44:29.647 回答
1

试试这样...

SqlCommand cmdInsCourses = 
       new SqlCommand("select * from CourseTable where InstructorID=@cur", con);
cmdInsCourses.Parameters.Add("cur", SqlDbType.VarChar).Value = cur;

编辑:问题在于您正在向 cmd 添加参数,而不是向 cmdInsCourses 添加参数,我已经更改了代码,只需查看一下即可。

于 2013-06-05T10:45:30.413 回答