.net 将Datatable绑定到Datagridview
对于我下面的代码,您是否有任何可能需要改进性能的建议?这是.NET2.0框架,DataTable被绑定到DataGridview。数据通过.ReadXML()加载到Datatable中。它不是来自数据库。任何地方都可以有80到100k的记录。Terms()数组是用户为搜索传递的。所以“bob taco”应该是术语(0)=“bob”,术语(1)=“taco”。我有一个正则表达式,可以维护任何引用的术语。所以“bob taco”将位于数组的一个元素上。有更好的办法吗?我尝试使用Dataview,因为它有更好的性能,但看起来我不能使用like操作符。欢迎任何建议,我真的想加快一点.net 将Datatable绑定到Datagridview,.net,datagridview,datatable,.net,Datagridview,Datatable,对于我下面的代码,您是否有任何可能需要改进性能的建议?这是.NET2.0框架,DataTable被绑定到DataGridview。数据通过.ReadXML()加载到Datatable中。它不是来自数据库。任何地方都可以有80到100k的记录。Terms()数组是用户为搜索传递的。所以“bob taco”应该是术语(0)=“bob”,术语(1)=“taco”。我有一个正则表达式,可以维护任何引用的术语。所以“bob taco”将位于数组的一个元素上。有更好的办法吗?我尝试使用Dataview,因为
Public Function Search(ByVal Terms() As String, ByRef ResidentTBL As DataTable) As DataTable
'Dim t As Long = Now.Ticks
Dim j As Integer
Dim newdt As New DataTable("Users")
Dim newtable As New DataTable
newtable = ResidentTBL.Clone
Dim termsceiling As Integer
termsceiling = Terms.GetUpperBound(0)
Dim filterstr As String = String.Empty
Dim foundrows() As DataRow
Dim sortOrder As String = "displayname ASC"
Dim tempstr As String
For j = 0 To termsceiling
'remedy any invalid sql characters
tempstr = Terms(j).Trim.ToUpper
tempstr = tempstr.Replace("'", "''")
tempstr = tempstr.Replace("*", "")
tempstr = tempstr.Replace("%", "")
'assemble the sql query
filterstr = filterstr & _
"((column1 LIKE '" & tempstr & "%') OR " & _
"(column2 LIKE '" & tempstr & "%') OR " & _
"(column3 LIKE '" & tempstr & "%') OR " & _
"(column4 LIKE '" & tempstr & "%') OR " & _
"(column5 LIKE '" & tempstr & "%') OR " & _
"(column6 LIKE '" & tempstr & "%') OR " & _
"(column7 LIKE '" & tempstr & "%') OR " & _
"(column8 LIKE '" & tempstr & "%') OR " & _
"(column9 LIKE '" & tempstr & "%') OR " & _
"(column10 LIKE '" & tempstr & "%'))"
'if there are further iterations append an AND (user typed more than one term)
If termsceiling > 0 And j <> termsceiling Then
filterstr = filterstr & " AND "
End If
Next j
filterstr = "(" & filterstr & ")" 'wrap the entire query
foundrows = ResidentTBL.Select(filterstr, sortOrder)
For i = 0 To foundrows.Length - 1
newtable.ImportRow(foundrows(i))
Next i
newdt = newtable
'Begin Debugging Code:
't = Now.Ticks - t
'MessageBox.Show("Took " & (t / 10000000) & " seconds.")
'End Debugging Code:
Return newdt
End Function
Public函数搜索(ByVal Terms()作为字符串,ByRef ResidentTBL作为DataTable)作为DataTable
'Dim t As Long=现在。滴答声
作为整数的Dim j
Dim newdt作为新数据表(“用户”)
将新表设置为新数据表
newtable=ResidentTBL.Clone
Dim TERMS以整数形式绘制
termsciling=Terms.GetUpperBound(0)
Dim filterstr As String=String.Empty
Dim foundrows()作为数据行
Dim sortOrder As String=“displayname ASC”
作为字符串的Dim tempstr
对于j=0的情况下,终止
'修复任何无效的sql字符
tempstr=术语(j).Trim.ToUpper
tempstr=tempstr.Replace(“”,““””)
tempstr=tempstr.Replace(“*”,“”)
tempstr=tempstr.Replace(“%”,“”)
'组装sql查询
过滤器TR=过滤器TR&_
((第1列类似“&tempstr&“%”)或“&_
“(第2列,如“&tempstr&”%”或“&_
“(第3列,如“&tempstr&”%”或“&_
“(第4列,如“&tempstr&”%”或“&_
“(第5列,如“&tempstr&”%”或“&_
“(第6列,如“&tempstr&”%”或“&_
“(第7列,如“&tempstr&”%”或“&_
“(第8列,如“&tempstr&”%”或“&_
“(第9列,如“&tempstr&”%”或“&_
“(第10列,如“&tempstr&“%”)
'如果有进一步的迭代,则追加AND(用户键入多个术语)
如果termsceiling>0且j termsceiling,则
filterstr=filterstr&“和”
如果结束
下一个j
filterstr=“(“&filterstr&”)包装整个查询
foundrows=ResidentTBL.Select(filterstr、sortOrder)
如果i=0,则查找行。长度为-1
newtable.ImportRow(foundrows(i))
接下来我
newdt=newtable
'开始调试代码:
't=Now.Ticks-t
'MessageBox.Show(“花费”和(t/10000000)和“秒”)
'结束调试代码:
返回newdt
端函数
我会在两个帐户上执行不同的操作:首先,我会在数据库上运行查询,并仅用正确(过滤)的数据填充datatable
其次,这种使用LIKE的方式每次都会遍历所有记录,如果用户太多,速度就会变慢。在这种情况下,我最终要做的是实现一个“穷人搜索引擎”,它基本上将这些字段中的每个文本解析为单词,并将每个单词插入另一个表中,每个单词都带有相应的用户ID。该表可以被索引,搜索将直接使用“=”而不是“喜欢”,从而加快搜索速度
编辑:没有数据库会使事情复杂化。特别是因为你有很多数据,我不知道在这样搜索时是否有索引或优化。如果您有一种在请求之间缓存datatable的方法,那么可以使用解析的数据构建另一个datatable。只要用户正在搜索相同类型的标记,它就应该可以工作,但是为了找到“bob taco”作为两个单词,一个挨着另一个,您需要在解析数据并进行相应搜索时保存单词的位置(这会使它变得更复杂)
例如:
ID, Text
1, Hangs out at Bob Taco joint
2, Hates Bob and his taco
我会给出这样的信息:
ID, Key, Pos
1, Hangs, 1
1, out, 2
1, at, 3
1, Bob, 4
1, Taco, 5
1, joint, 6
2, Hates, 1
2, Bob, 2
2, and, 3
2, his, 4
2, taco, 5
因此,现在您需要搜索包含bob和taco的ID,它们的Pos值之间的差值应该是1。例如,由于Pos为2和5,因此不应找到ID 2
我使用SQL中的临时表完成了这项工作。如果只需要在内存中工作,可能会变得更加困难。我会在两个帐户上执行不同的操作:首先,我会在数据库上运行查询,并仅使用正确的(过滤的)数据填充datatable 其次,这种使用LIKE的方式每次都会遍历所有记录,如果用户太多,速度就会变慢。在这种情况下,我最终要做的是实现一个“穷人搜索引擎”,它基本上将这些字段中的每个文本解析为单词,并将每个单词插入另一个表中,每个单词都带有相应的用户ID。该表可以被索引,搜索将直接使用“=”而不是“喜欢”,从而加快搜索速度 编辑:没有数据库会使事情复杂化。特别是因为你有很多数据,我不知道在这样搜索时是否有索引或优化。如果您有一种在请求之间缓存datatable的方法,那么可以使用解析的数据构建另一个datatable。只要用户正在搜索相同类型的标记,它就应该可以工作,但是为了找到“bob taco”作为两个单词,一个挨着另一个,您需要在解析数据并进行相应搜索时保存单词的位置(这会使它变得更复杂) 例如:
ID, Text
1, Hangs out at Bob Taco joint
2, Hates Bob and his taco
我会给出这样的信息:
ID, Key, Pos
1, Hangs, 1
1, out, 2
1, at, 3
1, Bob, 4
1, Taco, 5
1, joint, 6
2, Hates, 1
2, Bob, 2
2, and, 3
2, his, 4
2, taco, 5
因此,现在您需要搜索包含bob和taco的ID,它们的Pos值之间的差值应该是1。例如,由于Pos为2和5,因此不应找到ID 2
我使用SQL中的临时表完成了这项工作。如果只需要在内存中工作,可能会变得更加困难。我已经检查了一些问题,并且