2015-02-23 68 views
0
Curriculum table 
=== 
CurriculumID (PK, auto-increment) 
CurriculumName Varchar(255) 

GradeLevel table 
=== 
GradeLevelID (PK, auto-increment) 
GradeLevel Varchar(255) 
CurriculumID (foreign key to Curriculum) 

SubjectCreator table 
=== 
SubjectID(PK, auto-increment) 
SubjectName Varchar(255) 
SubjectDescription Varchar(255) 
GradeLevelID (foreign key to GradeLevel) 

多个表中的数据迄今为止,这些都是我的代码 我不知道查询显示3个表。只试过2个表格。 这些是2个表的代码。 string sql =“从GradeLevelCreator中选择GradeLevelID,GradeLevelName,CurriculumName GradeLevelCreator.CurriculumID = CurriculumCreator.CurriculumID”内部连接CurriculumCreator;如何显示在DataGridView中

string connectionString = "Data Source=TOSHIBA-PC\\KENNETH;Initial Catalog=HSPAEnrollmentSystem;Integrated Security=True"; 
string sql = ""; 
SqlConnection connection = new SqlConnection(connectionString); 
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection); 
DataSet ds = new DataSet(); 
connection.Open(); 
dataadapter.Fill(ds, "GradeLevelCreator"); 
connection.Close(); 
dataGridView1.DataSource = ds; 
dataGridView1.DataMember = "GradeLevelCreator"; 

我想我只是需要查询来显示多个或3个表。先谢谢你!

回答

0

试试这个SQL语句:

SELECT s.SubjectID, s.SubjectName, s.SubjectDescription, 
g.GradeLevel, c.CurriculumName FROM SubjectCreator s 
INNER JOIN GradeLevel g ON s.GradeLevelID = g.GradeLevelID 
INNER JOIN Curriculum c ON g.CurriculumID = c.CurriculumID