2011-04-28 27 views
0

我目前有一个名为Attendance的SQL表和一个名为Student的表。考勤表具有以下字段:AttendanceID,Date,Present,StudentID和Module ID。我的学生表具有StudentID和Name字段。我的应用程序中的一个页面允许用户在文本框中输入学生ID,其中来自考勤表的所有日期,现在的ModuleID都显示在Gridview中,以显示在文本框中输入的相应StudentID。这是到目前为止我的代码,其工作原理:根据另一个表中记录的ID在GridView中显示另一个表字段

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>" 
        SelectCommand="SELECT * FROM [Attendance] WHERE ([StudentID] = @StudentID)"> 
        <SelectParameters> 
         <asp:ControlParameter ControlID="pnumTextBox" Name="StudentID" 
          PropertyName="Text" Type="String" /> 
        </SelectParameters> 
       </asp:SqlDataSource> 

       <asp:GridView ID="GridView1" runat="server" 
        style="position:absolute; top: 241px; left: 357px; width: 356px;" 
        AutoGenerateColumns="False" DataKeyNames="AttendanceID" 
        DataSourceID="SqlDataSource1"> 
        <Columns> 
         <asp:BoundField DataField="AttendanceID" HeaderText="AttendanceID" 
          InsertVisible="False" ReadOnly="True" SortExpression="AttendanceID" /> 
         <asp:CheckBoxField DataField="Present" HeaderText="Present" 
          SortExpression="Present" /> 
         <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" /> 
         <asp:BoundField DataField="StudentID" HeaderText="StudentID" 
          SortExpression="StudentID" /> 
         <asp:BoundField DataField="ModuleID" HeaderText="ModuleID" 
          SortExpression="ModuleID" /> 
        </Columns> 
       </asp:GridView> 

随着显示AttendanceID,StudentID,的moduleId,现在和日期,我需要显示从学生表具有相同ID的学生姓名字段中输入在文本框中,我该如何实现这一目标?我认为可以在SELECT命令中做到这一点,但我不确定如何。任何帮助表示感谢,提前致谢!

回答

0
SELECT A.AttendanceID,A.Date,A.Present,A.ModuleID,S.StudentID,S.Name 
FROM attendance A ,student S 
WHERE A.StudentID = S.StudentID 
AND S.StudentID = "Your Value from the textbox"; 

INNER JOIN

我更喜欢使用objectDataSource而非SQLDataSource。 来分隔你的layers.and使用parameterized query来避免sql注入和验证用户输入。

+1

好的感谢您的回答,但我只是一个初学者,当谈到asp.net和MySQL你是什么意思使用参数化查询,以避免SQL注入?谢谢 – user715115 2011-04-28 10:37:38

+0

你会一天一天地学习所有这些概念,不用担心,我的意思是使用参数而不是写一个硬性查询。 Look to this link: http://www.databasejournal.com/features/mssql/article.php/3834501/Parameterized-Queries.htm – 2011-04-28 10:52:02

相关问题