2011-04-28 24 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

相關問題