0

我在这个gridview 的每一行都有一个gridview 和linkbuttons。当我单击其中一个按钮时,我会得到该行的 courseName、courseCode 和术语列值的值。这是我的代码:

 int row = -1;
        int.TryParse(e.CommandArgument as string, out row);

        GridViewRow gdrow = instCourses.Rows[row];

        // *** Get the underlying data item - in this case a DataRow
        DataRow dr = ((DataTable)this.instCourses.DataSource).Rows[gdrow.DataItemIndex];

        // *** Retrieve our context
        string courseCode = dr["CourseCode"].ToString();
        string courseNumber = dr["CourseNumber"].ToString();
        string term = dr["Term"].ToString();
        SqlConnection con = new SqlConnection();
        con.ConnectionString = Userfunctions.GetConnectionString();
        con.Open();
        SqlCommand cmd=new SqlCommand("select RegisterTable.StudentID,StudentTable.Name,StudentTable.Surname from RegisterTable INNER JOIN StudentTable where RegisterTable.StudentID=StudentTable.StudentID and RegisterTable.CourseCode=@courseCode and RegisterTable.Term=@term and RegisterTable.CourseNumber=@courseNumber",con);
        cmd.Parameters.AddWithValue("@courseCode", courseCode);
        cmd.Parameters.AddWithValue("@courseNumber", courseNumber);
        cmd.Parameters.AddWithValue("@term", term);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        studList.DataSource = dt;
        studList.DataBind();

这些值按我的预期返回,但我认为我的查询有问题,它在关键字“where”附近显示语法不正确。任何人都可以看到问题吗?

谢谢

4

5 回答 5

5

格式化你的sql查询,你会看到问题:

select RegisterTable.StudentID,StudentTable.Name,StudentTable.Surname 
from RegisterTable INNER JOIN StudentTable 
where RegisterTable.StudentID=StudentTable.StudentID 
and RegisterTable.CourseCode=@courseCode 
and RegisterTable.Term=@term 
and RegisterTable.CourseNumber=@courseNumber

连接中没有ON子句。

from RegisterTable INNER JOIN StudentTable 
    ON RegisterTable.StudentID=StudentTable.StudentID 
where ....

顺便说一句,在 C# 中,您可以使用逐字字符串文字来格式化您的 sql:

string sql = @"
    SELECT registertable.studentid, 
           studenttable.name, 
           studenttable.surname 
    FROM   registertable 
           INNER JOIN studenttable 
                   ON registertable.studentid = studenttable.studentid 
    WHERE  registertable.coursecode = @courseCode 
           AND registertable.term = @term 
           AND registertable.coursenumber = @courseNumber";

然后更容易在你的 sql 查询中找到错误。

于 2013-06-05T13:25:48.503 回答
1

查询语法不正确。尝试:

SELECT 
    RegisterTable.StudentID,
    StudentTable.Name,
    StudentTable.Surname 
FROM RegisterTable 
INNER JOIN StudentTable ON RegisterTable.StudentID = StudentTable.StudentID
WHERE 
    RegisterTable.CourseCode = @courseCode 
    AND RegisterTable.Term = @term 
    AND RegisterTable.CourseNumber = @courseNumber

您需要将 更改WHEREON以完成JOIN,然后您应该将其余条件移至WHERE子句。

于 2013-06-05T13:27:11.067 回答
1

你有一个不带 ON 的内部连接

select RegisterTable.StudentID,StudentTable.Name,StudentTable.Surname from RegisterTable INNER JOIN StudentTable ON RegisterTable.studentid = StudentTable .id  where RegisterTable.StudentID=StudentTable.StudentID and RegisterTable.CourseCode=@courseCode and RegisterTable.Term=@term and RegisterTable.CourseNumber=@courseNumber
于 2013-06-05T13:24:49.400 回答
1

您在这里缺少加入条件

FROM RegisterTable INNER JOIN StudentTable

您的内部连接应如下所示

from RegisterTable INNER JOIN StudentTable 
   ON RegisterTable.StudentID = StudentTable.StudentId

根据您的 where 子句,您的查询可以在不使用内部连接的情况下编写,只需使用笛卡尔积即可。

select 
        RegisterTable.StudentID,
        StudentTable.Name,
        StudentTable.Surname 
from    RegisterTable, StudentTable 
where   RegisterTable.StudentID=StudentTable.StudentID and 
        RegisterTable.CourseCode=@courseCode and 
        RegisterTable.Term=@term and 
        RegisterTable.CourseNumber=@courseNumber
于 2013-06-05T13:24:57.577 回答
1

您需要指定每个表中要加入StudentTable的列,ON而不是WHERE

SELECT RegisterTable.StudentID, StudentTable.Name, StudentTable.Surname
FROM RegisterTable
INNER JOIN StudentTable ON RegisterTable.StudentID = StudentTable.StudentID
WHERE RegisterTable.CourseCode = @courseCode
    AND RegisterTable.Term = @term
    AND RegisterTable.CourseNumber = @courseNumber
于 2013-06-05T13:27:57.873 回答