天天看點

聯表查詢     聯表查詢

 聯表查詢

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

開發工具與關鍵技術:VS、聯表查詢

作者:#33

撰寫時間:撰寫時間:2019年04月14日

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

在老師MVC教學中學習查詢多張表資料的知識,首先進行表格渲染—在控制器寫查詢需要的字段—最後在視圖表格渲染的配置項上添加路徑---url:‘方法名’。

聯表查詢     聯表查詢

在表格渲染時加入配置項,可以為表格添加工具欄---toolbar:“#toolbarDemo”;

工具欄有自帶的功能:篩選列、導出、列印,也可以添加自己需要的其他功能。

<script type="text/html" id="toolbarDemo">

    <div class="layui-btn-container">

        <button class="layui-btn layui-btn-sm" onclick="one()">功能1</button>

        <button class="layui-btn layui-btn-sm" onclick="two()">功能2</button>

        <button class="layui-btn layui-btn-sm" onclick="three()">功能3</button>

    </div>

</script>

查詢學生資訊關聯多張表的資料,此時需要把這些表對應的主鍵與外鍵連接配接,還要對得到的資料進行分頁處理,是以寫查詢的方法還要加上分頁的參數。

聯表查詢     聯表查詢

經典代碼:

(學生表包含其他表的主鍵ID時)聯表查詢,學生表的學院ID=學院表的學院ID;學生表的專業ID=專業表的專業ID;學生表的年級ID=年級表的年級ID;學生表的班級ID=班級表的班級ID;學生表的使用者ID=使用者表的使用者ID。

where語句判斷使用者是否廢棄。

public ActionResult SELStudent(LayuiTablePage layuiTablePage) {—定義分頁實體類

var listStudent = (from tbStudent in myModel.PW_Student

    join tbAcademe in myModel.SYS_Academe on tbStudent.AcademeID equals tbAcademe.AcademeID

    join tbSpecialty in myModel.SYS_Specialty on tbStudent.SpecialtyID equals tbSpecialty.SpecialtyID

    join tbGrade in myModel.SYS_Grade on tbStudent.GradeID equals tbGrade.GradeID

    join tbClass in myModel.SYS_Class on tbStudent.ClassID equals tbClass.ClassID

    join tbUser in myModel.PW_User on tbStudent.UserID equals tbUser.UserID

    where tbUser.ToVoidNo==true

    select new StudentInfor{——給查詢的字段指派

               studentID = tbStudent.studentID,

               StudentNumber = tbStudent.StudentNumber,

               StudentName = tbStudent.StudentName,

               StudentIDNum = tbStudent.StudentIDNum,

               StudentSex = tbStudent.StudentSex,

               AcademeID = tbStudent.AcademeID,

               Academe = tbAcademe.AcademeName,

               Specialty = tbSpecialty.SpecialtyName,

               GradeID = tbStudent.GradeID,

               Grade = tbGrade.GradeName,

               ClassID = tbStudent.ClassID,

               Class = tbClass.ClassName,

               UserNuber = tbUser.UserNuber                           

               }).ToList();— ———————查詢多條資料

   int totals = listStudent.Count();————定義一個變量記錄查詢資料的總條數

   List<StudentInfor> list = listStudent.OrderBy(m => m.studentID)

   .Skip(layuiTablePage.GetStartIndex()).Take(layuiTablePage.limit).ToList();

   LayuiTableData<StudentInfor>layuiTableData=newLayuiTableData<StudentInfor>();

            layuiTableData.count = totals;

            layuiTableData.data = list;

            return Json(layuiTableData, JsonRequestBehavior.AllowGet);

        }

在視圖中表格渲染的url添加控制器的方法名(資料接口);

聯表查詢     聯表查詢

繼續閱讀