第八周学习笔记
一、数据行
1、 利用逗号分隔将数据导入数据库中
将excel保存为后缀为.csv的文件
在数据库中插入表时跟之前有一点不同
BULK INSERT tb_Course
FROM 'C:\Course.csv'
WITH
(FIELDTERMINATOR=',' → 每个数据中间是用逗号隔开的
,ROWTERMINATOR='\n'
,FIRSTROW=2); → 插入的第一行从第二排开始
BULK:批量插入。此方法插入数据优点是方便、快。缺点是,当插入的数据中有逗号时,会出错。
2、 计算所选课程的总分
利用Compute计算总分,代码如下:
1 this.lbl_CreditSum.Text = 2 3 "共" + this.SelectedCourseTable.Compute("SUM(Credit)", "").ToString() + "学分";
3、 添加选课
将可选的课程添加到已选课程中,并在可选课程中删除被选择了的课程。利用 delete 功能,其实delete并没有真正将数据删除,只是在前端隐藏。代码如下:
1 if (this.dgv_Course.RowCount > 0) { 2 3 DataRow 4 5 currentCourseRow = ((DataRowView)this.dgv_Course.CurrentRow.DataBoundItem).Row , selectedCourseRow = this.SelectedCourseTable.NewRow(); 6 7 selectedCourseRow["No"] = currentCourseRow["No"]; 8 9 selectedCourseRow["Name"] = currentCourseRow["Name"]; 10 11 selectedCourseRow["Credit"] = currentCourseRow["Credit"]; 12 13 this.SelectedCourseTable.Rows.Add(selectedCourseRow); 14 15 currentCourseRow.Delete();
4、 退选新增的行
可退选尚未添加到数据库中的课程。 这个时候我们就要用上之前的delete功能,恢复原样
1 if (this.dgv_SelectedCourse.RowCount > 0) 2 3 { 4 5 DataRow selectedCourseRow = 6 7 ((DataRowView)this.dgv_SelectedCourse.CurrentRow.DataBoundItem).Row; 8 9 if (selectedCourseRow.RowState == DataRowState.Unchanged) 10 11 { 12 13 return; 14 15 } 16 17 else 18 19 { 20 21 string courseNo = selectedCourseRow["No"].ToString(); 22 23 DataRow deletedCourseRow = 24 this.CourseTable.Select("No='" + courseNo + "'", "", DataViewRowState.Deleted)[0];
this.SelectedCourseTable.Rows.Remove(selectedCourseRow);
deletedCourseRow.RejectChanges();
然后删除已选课程中被删除的:
1 this.SelectedCourseTable.Rows.Remove(selectedCourseRow);
二、分页
1、创建一张表,并准备好列
1 sqlCommand.CommandText = 2 3 "SELECT S.No,S.Name AS SName,C.Name AS CName,SS.TotalScore" 4 5 + " FROM tb_Student AS S " 6 7 + " JOIN tb_StudentScore AS SS ON S.No=SS.StudentNo" 8 9 + " JOIN tb_Course AS C ON SS.CourseNo=C.No" 10 11 + " WHERE 1=0;"; //指定SQL命令的命令文本;该命令查询所有学生成绩,但只返回空表,即只获取架构;
2、 设置数据列的类型;类型需借助typeof获取
1 rowIdColumn.DataType = typeof(int); 2 3 4 5 rowIdColumn.AutoIncrement = true; //设置数据列为自增列; 6 7 rowIdColumn.AutoIncrementSeed = 1; //设置数据列的自增种子为1; 8 9 rowIdColumn.AutoIncrementStep = 1; //设置数据列的自增步长为1;
3、利用数据适配器填充数据:
1 sqlDataAdapter.Fill(this.CourseTable); //SQL数据适配器读取数据,并填充课程数据表;
5、 翻页
上一页, 代码如下:
1 private void btn_PreviosPage_Click(object sender, EventArgs e) 2 3 { 4 5 if (this.CurrentPageNo > 1) //若当前页号大于1; 6 7 { 8 9 this.CurrentPageNo--; //则当前页号递减; 10 11 } 12 13 this.CurrentPageView.RowFilter = //设置课程数据视图的行筛选条件,即筛选当前页的记录; 14 15 "RowID >" + (this.CurrentPageNo - 1) * this.PageSize 16 17 + " AND RowID <=" + this.CurrentPageNo * this.PageSize; //根据当前页号、每页大小,计算相应的行编号范围,并作为行筛选条件; 18 19 }
下一页,代码如下:
1 private void btn_NextPage_Click(object sender, EventArgs e) 2 3 { 4 5 if (this.CurrentPageNo < this.MaxPageNo) //若当前页号尚未超出最大页号; 6 7 { 8 9 this.CurrentPageNo++; //则当前页号递增; 10 11 } 12 13 this.CurrentPageView.RowFilter = //设置课程数据视图的行筛选条件,即筛选当前页的记录; 14 15 "RowID >" + (this.CurrentPageNo - 1) * this.PageSize 16 17 + " AND RowID <=" + this.CurrentPageNo * this.PageSize; //根据当前页号、每页大小,计算相应的行编号范围,并作为行筛选条件; 18 19 }