以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- 后台数据排序 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=110292) |
-- 作者:刘林 -- 发布时间:2017/11/30 14:51:00 -- 后台数据排序 Dim ksmc As String = e.Form.Controls("combobox1").text If ksmc > "" Dim dt As DataTable = DataTables("成绩") Dim qArys As List(Of String()) = dt.GetValues("考试名称|年级代码") If js.Contains("区业务主管") For Each qAry As String() In qArys Dim drs As List(Of DataRow) = dt.S elect("[考试名称] = \'" & qary(0) & "\'And [年级代码] =\'" & qary(1) & "\' and 学段 =\'初中\'", "折总 DESC") For n As Integer = 0 To drs.Count - 1 \'遍历所有行 If n > 0 AndAlso drs(n)("折总") = drs(n-1)("折总") Then \'如果总分和上一行相同 drs(n)("区序") = drs(n-1)("区序") \'则排名等于上一行 Else drs(n)("区序") = n + 1 \'设置排名 End If Next Next For Each qAry As String() In qArys Dim drs1 As List(Of DataRow) = dt.Sele ct("[考试名称] = \'" & qary(0) & "\'And [年级代码] =\'" & qary(1) & "\'and 学段=\'初中\'", "总分 DESC") For n As Integer = 0 To drs1.Count - 1 \'遍历所有行 If n > 0 AndAlso drs1(n)("总分") = drs1(n-1)("总分") Then \'如果总分和上一行相同 drs1(n)("总分区序") = drs1(n-1)("总分区序") \'则排名等于上一行 Else drs1(n)("总分区序") = n + 1 \'设置排名 End If Next Next End If Dim Arys As List(Of String()) = dt.GetValues("考试名称|单位名称|年级代码") For Each Ary As String() In Arys Dim drs As List(Of DataRow) = dt.Select("[考试名称] = \'" & ary(0) & "\'And [单位名称] = \'" & ary(1) & "\'and [年级代码] =\'" & ary(2) & "\'and 学段=\'初中\'", "折总 DESC") For n As Integer = 0 To drs.Count - 1 \'遍历所有行 If n > 0 AndAlso drs(n)("折总") = drs(n-1)("折总") Then \'如果总分和上一行相同 drs(n)("校序") = drs(n-1)("校序") \'则排名等于上一行 Else drs(n)("校序") = n + 1 \'设置排名 End If Next Next For Each Ary As String() In Arys Dim drs As List(Of DataRow) = dt.Sele ct("[考试名称] = \'" & ary(0) & "\'And [单位名称] = \'" & ary(1) & "\'and [年级代码] =\'" & ary(2) & "\'and 学段 = \'初中\'", "总分 DESC") For n As Integer = 0 To drs.Count - 1 \'遍历所有行 If n > 0 AndAlso drs(n)("总分") = drs(n-1)("总分") Then \'如果总分和上一行相同 drs(n)("总分校序") = drs(n-1)("总分校序") \'则排名等于上一行 Else drs(n)("总分校序") = n + 1 \'设置排名 End If Next Next Dim Arys1 As List(Of String()) = dt.GetValues("考试名称|单位名称|年级代码|班级") For Each Ary1 As String() In Arys1 Dim drs As List(Of DataRow) = dt.Se lect("[考试名称] = \'" & ary1(0) & "\'And [单位名称] = \'" & ary1(1) & "\'and [年级代码] =\'" & ary1(2) & "\'And [班级] = \'" & ary1(3) & "\'and 学段=\'初中\'", "折总 DESC") For m As Integer = 0 To drs.Count - 1 \'遍历所有行 If m > 0 AndAlso drs(m)("折总") = drs(m-1)("折总") Then \'如果总分和上一行相同 drs(m)("班序") = drs(m-1)("班序") \'则排名等于上一行 Else drs(m)("班序") = m + 1 \'设置排名 End If Next Next For Each Ary1 As String() In Arys1 Dim drs As List(Of DataRow) = dt.Sel ect("[考试名称] = \'" & ary1(0) & "\'And [单位名称] = \'" & ary1(1) & "\'and [年级代码] =\'" & ary1(2) & "\'And [班级] = \'" & ary1(3) & "\'and 学段=\'初中\'", "总分 DESC") For m As Integer = 0 To drs.Count - 1 \'遍历所有行 If m > 0 AndAlso drs(m)("总分") = drs(m-1)("总分") Then \'如果总分和上一行相同 drs(m)("总分班序") = drs(m-1)("总分班序") \'则排名等于上一行 Else drs(m)("总分班序") = m + 1 \'设置排名 End If Next Next Dim strlist As List(Of String) = Functions.Execute("sql提取函数",Tables("成绩"),"成绩","_Identify","[_Identify]","班序|校序|区序|总分班序|总分校序|总分区序","班序|校序|区序|总分班序|总分校序|总分区序",1) Dim sql As String Dim cmd As New SQLCommand cmd.C Dim Count As Integer = 0 try Connections").BeginTransaction For Each s As String In strlist cmd.CommandText = s Count += cmd.ExecuteNonQuery() Next Connections").Commit \'提交事务,所有操作生效 Catch ex As Exception \'如果出错 msgbox(ex.message) Connections(").Rollback() \'回滚事务,撤销所有操作 End Try Tables("成绩").DataTable.BaseTable.AcceptChanges() \'提交修改 Else messagebox.show("请选择要排的考试名称","提示") End If 老师,为进行后台数据排序,为提高速度,我采取加载后排序再用sql提取保存,但还是很慢,5000左右记录要等一分钟左右,有什么办法直接进行后台排序,sqlreplacefor 写入排序结果
|
-- 作者:有点甜 -- 发布时间:2017/11/30 15:05:00 -- 先计算排名用了多少时间,再计算保存用了多少时间,看是哪里有问题。
如果是排名的问题,你的代码还可以进一步优化;如果是保存的问题,那没办法了。 |
-- 作者:有点甜 -- 发布时间:2017/11/30 15:06:00 -- 具体问题单独做个例子发上来说明。 |
-- 作者:刘林 -- 发布时间:2017/11/30 15:27:00 -- 我测了,将一次考试加载后排序很短,一两秒,但存太久了 能不能用sql语句实现后台排序 |
-- 作者:有点蓝 -- 发布时间:2017/11/30 16:22:00 -- 用合成sql保存数据 |
-- 作者:有点甜 -- 发布时间:2017/11/30 21:15:00 -- 以下是引用刘林在2017/11/30 15:27:00的发言:
我测了,将一次考试加载后排序很短,一两秒,但存太久了 能不能用sql语句实现后台排序
做个实例发上来测试吧。 |
-- 作者:刘林 -- 发布时间:2017/12/1 12:11:00 -- 是用的sql保存 Dim strlist As List(Of String) = Functions.Execute("sql提取函数",Tables("成绩"),"成绩","_Identify","[_Identify]","班序|校序|区序|总分班序|总分校序|总分区序","班序|校序|区序|总分班序|总分校序|总分区序",1) Dim sql As String Dim cmd As New SQLCommand cmd.C Dim Count As Integer = 0 try Connections").BeginTransaction For Each s As String In strlist cmd.CommandText = s Count += cmd.ExecuteNonQuery() Next Connections").Commit \'提交事务,所有操作生效 Catch ex As Exception \'如果出错 msgbox(ex.message) Connections(").Rollback() \'回滚事务,撤销所有操作 End Try Tables("成绩").DataTable.BaseTable.AcceptChanges() \'提交修改 关键是还是很慢
|
-- 作者:有点甜 -- 发布时间:2017/12/1 14:30:00 -- 加上msgbox代码,看看那个语句执行慢。
msgbox(1) Dim strlist As List(Of String) = Functions.Execute("sql提取函数",Tables("成绩"),"成绩","_Identify","[_Identify]","班序|校序|区序|总分班序|总分校序|总分区序","班序|校序|区序|总分班序|总分校序|总分区序",1) msgbox(2)
-------------
Connections("").BeginTransaction For Each s As String In strlist
cmd.CommandText = s
Count += cmd.ExecuteNonQuery()
Next
msgbox(3)
Connections("").Commit \'提交事务,所有操作生效
msgbox(4)
|