我有一个应用程序,其中l将学生成绩存储在27个表中。从1 - 9年级。每个年级有3个不同的表来存储第一,第二和第三学期的成绩,因此有27个表。所以我的问题是如何从1 - 9年级的学生中选择所有成绩,并使用学生ID作为查询的唯一标识符进行显示。
private void btnsearch_Click(object sender, EventArgs e)
{
cn.Open();
cmd = new SqlCommand("select * from grade1FT_results INNER JOIN grade1ST_results,grade1TT_results,grade2FT_results,grade2ST_results,grade2TT_results,grade3FT_results,grade3ST_results,grade3TT_results,grade4FT_results,grade4ST_results,grade4TT_results,grade5FT_results,grade5ST_results,grade5TT_results,grade6FT_results,grade6ST_results,grade6TT_results,grade7FT_results,grade7ST_results,grade7TT_results,grade8FT_results,grade8ST_results,grade8TT_results,grade9FT_results,grade9ST_results,grade9TT_results ON StudentId=@Id ", cn);
cmd.Parameters.AddWithValue("@Id", txtid.Text);
cmd.ExecuteNonQuery();
DataTable dtable;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
dtable = new DataTable();
da.Fill(dtable);
BindingSource dsource = new BindingSource();
dsource.DataSource = dtable;
datagrid.DataSource = dsource;
da.Update(dtable);
cn.Close();
}
我收到的错误是语法不正确:"语法不正确,','。" 在这一行
cmd.ExecuteNonQuery();
非常感谢任何帮助。
答案 0 :(得分:1)
就您所遇到的问题而言,您的SQL不正确。这就是你得到那个错误的原因。使用SQL Server Management Studio测试SQL而不是从应用程序执行此操作;这将节省你这么多时间。
我强烈建议在SQL Server中使用VIEW来执行此操作。我使用LEFT JOINs,因为你可能希望结果中的学生在10,11或12年级之前没有上学,所以我认为没有记录。我认为这非常接近:
CREATE VIEW dbo.StudentG1toG9
AS
select * --You need to list out each column here (StudentID only once)
from Student
LEFT JOIN grade1FT_results AS [G1FT] ON [G1FT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade1ST_results AS [G1ST] ON [G1ST].[StudentID]=[Student].[StudentID]
LEFT JOIN grade1TT_results AS [G1TT] ON [G1TT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade2FT_results AS [G2FT] ON [G2FT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade2ST_results AS [G2ST] ON [G2ST].[StudentID]=[Student].[StudentID]
LEFT JOIN grade2TT_results AS [G2TT] ON [G2TT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade3FT_results AS [G3FT] ON [G3FT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade3ST_results AS [G3ST] ON [G3ST].[StudentID]=[Student].[StudentID]
LEFT JOIN grade3TT_results AS [G3TT] ON [G3TT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade4FT_results AS [G4FT] ON [G4FT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade4ST_results AS [G4ST] ON [G4ST].[StudentID]=[Student].[StudentID]
LEFT JOIN grade4TT_results AS [G4TT] ON [G4TT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade5FT_results AS [G5FT] ON [G5FT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade5ST_results AS [G5ST] ON [G5ST].[StudentID]=[Student].[StudentID]
LEFT JOIN grade5TT_results AS [G5TT] ON [G5TT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade6FT_results AS [G6FT] ON [G6FT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade6ST_results AS [G6ST] ON [G6ST].[StudentID]=[Student].[StudentID]
LEFT JOIN grade6TT_results AS [G6TT] ON [G6TT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade7FT_results AS [G7FT] ON [G7FT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade7ST_results AS [G7ST] ON [G7ST].[StudentID]=[Student].[StudentID]
LEFT JOIN grade7TT_results AS [G7TT] ON [G7TT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade8FT_results AS [G8FT] ON [G8FT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade8ST_results AS [G8ST] ON [G8ST].[StudentID]=[Student].[StudentID]
LEFT JOIN grade8TT_results AS [G8TT] ON [G8TT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade9FT_results AS [G9FT] ON [G9FT].[StudentID]=[Student].[StudentID]
LEFT JOIN grade9ST_results AS [G9ST] ON [G9ST].[StudentID]=[Student].[StudentID]
LEFT JOIN grade9TT_results AS [G9TT] ON [G9TT].[StudentID]=[Student].[StudentID]
GO
然后你可以从代码中得到它:
SELECT *
FROM dbo.StudentG1toG9
WHERE [StudentID]=@Id
答案 1 :(得分:0)
从代码中删除cmd.ExecuteNonQuery();
代码行。
ExecuteNonQuery()
:对连接执行Transact-SQL语句(如UPDATE,INSERT或DELETE)并返回受影响的行数。但是不要返回结果集。