如何可以提高速度?
一个LISTVIEW
一个DATASET
从listview中读取得到相应值并以for .... next填充到DATASET
但是200条数据就用了10秒~不知道有没有方法让他可以更快些?
[解决办法]
200条数据用了10秒??怀疑代码问题~~
[解决办法]
一般这种转换都是特殊对应,很难直接绑定的。大多数都是使用这种循环填充的方法。
速度不会太慢
[解决办法]
ListView控件填充数据是挺慢的,还不如使用DataGridView控件呢。
[解决办法]
肯定是代码写的有问题,贴代码吧
[解决办法]
用数据绑定的方式来处理
[解决办法]
问题应该在于你的频繁
Conn.Open。。。。Conn.Close
数据库的连接不能这么操作的,一般放在函数体内的长连接都不被人们采纳
何况你放在循环体内??
1、尽量函数不要用中文,看起来真别扭。
2、函数体开头一个Open,末尾一个Close足够了,C/S程序尽量使用长连接。
你的程序问题多多,建议多看看基础编程规范
[解决办法]
说句良心话——不是有意贬低,真的好久没有看到如此Laji的代码了。。。
汗一个。。。现在新手也都比较注意自己的编程习惯了。。。。
[解决办法]
PF楼主的写法
可能没有考虑过程序的健壮性,光考虑程序功能的完成性
汗一个,而且,看如此代码,估计是已经销售了的程序,为使用者默哀:)
[解决办法]
变量声明竟然没有类型,只有Dim i........全是Object的,转换也没有个标准
我终于知道你为什么速度如此之差了——这样的编程习惯,如果速度快起来
岂不反而使咄咄怪事??
姑且盲目的猜测一下,楼主是从VB转过来的,而且并不是新手——应该是某个小公司延续了很多垃圾代码的程序员。。。。然后继续制造垃圾。。。
其实并不是鄙视楼主个人——这种情况,害人的是这些近利的小公司,只要速度和蒙钱,代码是否规范,是否效率高,并不是他们考虑的~~
[解决办法]
连接别在循环中 OPEN CLOSE
把函数分解下,自己看的也方便,以后维护也容易不是
[解决办法]
数据库名称、函数名称中间不但有中文、拼音,还有些蹩脚的英文——你要能专注用一种也是好的啊。简直是不忍目睹。
[解决办法]
先把数据读取出来放在datatable里,再循环读取,这样速度会有所改善的
[解决办法]
和数据库打交道,一般是这样的
界面 <-> 缓存 <-> 数据库
如无必要,不要
界面 <-> 数据库
[解决办法]
我来贴贴我的一点代码
[解决办法]
Imports System.Data.OleDb
Public Class Database
Private Function GetConnection() As OleDbConnection
Return New OleDbConnection(My.MySettings.Default.BankPayConnectionString)
End Function
Public Function GetCommand(ByVal cmdtext As String) As OleDbCommand
Return New OleDbCommand(cmdtext, GetConnection)
End Function
Public Function GetDataAdapter(ByVal cmdText As String, Optional ByVal autoBuildOtherCmd As Boolean = False) As OleDbDataAdapter
Return GetDataAdapter(GetCommand(cmdText), autoBuildOtherCmd)
End Function
Public Function GetDataAdapter(ByVal cmd As OleDbCommand, Optional ByVal autoBuildOtherCmd As Boolean = False) As OleDbDataAdapter
Dim ad As New OleDbDataAdapter(cmd)
If autoBuildOtherCmd Then
Dim c As New OleDbCommandBuilder(ad)
End If
Return ad
End Function
Public Sub Fill(ByVal cmdtext As String, ByVal table As DataTable)
Using ad As OleDbDataAdapter = GetDataAdapter(cmdtext)
ad.Fill(table)
End Using
End Sub
Public Sub Fill(ByVal cmdtext As String, ByVal dataset As DataSet)
Using ad As OleDbDataAdapter = GetDataAdapter(cmdtext)
ad.Fill(dataset)
End Using
End Sub
Public Sub Fill(ByVal cmdtext As String, ByVal dataset As DataSet, ByVal srcTable As String)
Using ad As OleDbDataAdapter = GetDataAdapter(cmdtext)
ad.Fill(dataset, srcTable)
End Using
End Sub
Public Shared Function Instance() As Database
Return New Database
End Function
End Class
[解决办法]
我要处理的表很简单,程序是一个过渡型的工具:
Public Module Constant
Public Enum TableType
环境参数
银行帐号
营业所
用户
用户银行
银行
End Enum
Public Function GetSelect(ByVal tablename As TableType) As String
Return String.Format( "SELECT * FROM {0} ", tablename.ToString)
End Function
Public Enum FileType
TXT
XLS
OTHER
End Enum
End Module
[解决办法]
Public Module Enviroment
Public 营业所 As New 营业所
Public 环境参数 As New 环境参数
Public 银行 As New 银行
Public 银行帐号 As New 银行帐号
Public 用户 As New 用户
Public 用户银行 As New 用户银行
Public TableManager As BankTableCollection
Sub New()
With uWindows.ApplicationBase.Instance
Dim MainForm As Form = .WindowsFormsApplicationBase.ApplicationContext.MainForm
Dim ui As Object = uReflection.Members.GetMemberValue(MainForm, "NavigatorGrid1 ")
If ui IsNot Nothing Then
BankTableCollection.Defalut.UI = CType(ui, uForms.NavigatorGrid)
End If
End With
TableManager = BankTableCollection.Defalut
End Sub
End Module
[解决办法]
接
[解决办法]
Public MustInherit Class BankTableBase
Private gDataAdapter As OleDbDataAdapter
Private WithEvents gBindingSource As BindingSource
Private gTableInfo As uForms.uBindingNavigator.TableInformation
Private WithEvents gTable As DataTable
Private gCurrentRow As DataRow
Private gName As String
Public ReadOnly Property Name() As String
Get
Return gName
End Get
End Property
Public Overridable ReadOnly Property Table() As DataTable
Get
Return gTable
End Get
End Property
Public Overridable ReadOnly Property CurrentRow() As DataRow
Get
Return gCurrentRow
End Get
End Property
Public ReadOnly Property TableInfo() As uForms.uBindingNavigator.TableInformation
Get
Return gTableInfo
End Get
End Property
Public ReadOnly Property BindingSource() As BindingSource
Get
Return gBindingSource
End Get
End Property
Sub New()
gName = SetName()
gTable = CreateTable()
gBindingSource = New BindingSource(gTable, Nothing)
gTableInfo = New uForms.uBindingNavigator.TableInformation(gTable, gName)
gTableInfo.DataAdapter = CreateDataAdapter()
If Not BankTableCollection.Defalut.ContainsKey(Me.Name) Then
BankTableCollection.Defalut.Add(Me.Name, Me)
End If
End Sub
Protected MustOverride Function CreateTable() As DataTable
Protected MustOverride Function SetName() As String
Protected MustOverride Function CreateDataAdapter() As OleDbDataAdapter
Protected Overridable Sub ActionWhenNewRow(ByVal newrow As DataRow)
End Sub
Private Sub gBindingSource_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles gBindingSource.CurrentChanged
Dim row As DataRow
If gBindingSource.Current Is Nothing Then
row = gTable.NewRow
Else
row = CType(gBindingSource.Current, DataRowView).Row
End If
uReflection.Members.DataRowToItem(row, Me)
gCurrentRow = row
End Sub
Public Overridable Sub ThisItemAddToTable()
Me.Table.Rows.Add(Me.ThisItemCreateNewRow)
Me.BindingSource.MoveLast()
End Sub
Public Sub ThisItemDelete()
If Me.CurrentRow Is Nothing Then Return
Me.CurrentRow.Delete()
End Sub
Public Sub FillDataTable()
Me.TableInfo.Fill()
End Sub
Public Sub UpdateDataTable()
Me.TableInfo.Update()
End Sub
Public Sub ThisItemUpdateCurrentRow()
If Me.CurrentRow Is Nothing Then
ThisItemAddToTable()
Else
uReflection.Members.ItemUpdateDataRow(Me, Me.CurrentRow)
End If
End Sub
Protected Overridable Sub InternalReadFrom(ByVal file As String, ByVal filetype As Constant.FileType)
End Sub
Protected Overridable Sub InternalSaveAs(ByVal file As String, ByVal filetype As Constant.FileType)
End Sub
Public Sub ReadFrom(ByVal file As String, ByVal filetype As Constant.FileType)
InternalReadFrom(file, filetype)
End Sub
Public Sub SaveAs(ByVal file As String, ByVal filetype As Constant.FileType)
InternalSaveAs(file, filetype)
End Sub
Public Function ThisItemCreateNewRow() As DataRow
Return uReflection.Members.ItemToDataRow(Me, gTable)
End Function
Public Function [GoTo](ByVal propertyName As String, ByVal key As Object) As Integer
Dim position As Integer = FindRow(propertyName, key)
If position = -1 Then GoTo FINISH
gBindingSource.Position = position
FINISH:
Return position
End Function
Public Function FindRow(ByVal propertyName As String, ByVal key As Object) As Integer
Return gBindingSource.Find(propertyName, key)
End Function
Public Function FindRows(ByVal filter As String) As DataRow()
Return gTable.Select(filter)
End Function
Private Sub gTable_TableNewRow(ByVal sender As Object, ByVal e As System.Data.DataTableNewRowEventArgs) _
Handles gTable.TableNewRow
ActionWhenNewRow(e.Row)
End Sub
End Class
[解决办法]
Public Class BankTableCollection
Inherits Collections.Generic.Dictionary(Of String, BankTableBase)
Private gUI As uForms.NavigatorGrid
Private Shared gDefault As BankTableCollection
Public Shared ReadOnly Property [Defalut]() As BankTableCollection
Get
If gDefault Is Nothing Then
gDefault = New BankTableCollection
End If
Return gDefault
End Get
End Property
Public Property UI() As uForms.NavigatorGrid
Get
Return gUI
End Get
Set(ByVal value As uForms.NavigatorGrid)
gUI = value
End Set
End Property
Public Sub BindingAllToUI()
With UI.Navigator.SourceManager
.Clear()
For Each item As BankTableBase In Me.Values
.Add(item.TableInfo)
Next
End With
End Sub
Public Sub BindingToUI(ByVal key As String)
With UI.Navigator.SourceManager
.Clear()
.Add(Item(key).TableInfo)
End With
End Sub
Public Sub ClearUI()
With UI.Navigator.SourceManager
.Clear()
End With
End Sub
End Class
[解决办法]
其中的一个表类:
Public Class 用户
Inherits BankTableBase
Private g用户ID As Integer
Private g用户名称 As String
Private g用户编码 As String
Private g营业所编码 As String
Public Property 用户ID() As Integer
Get
Return g用户ID
End Get
Set(ByVal value As Integer)
g用户ID = value
End Set
End Property
Public Property 用户名称() As String
Get
Return g用户名称
End Get
Set(ByVal value As String)
g用户名称 = value
End Set
End Property
Public Property 用户编码() As String
Get
Return g用户编码
End Get
Set(ByVal value As String)
g用户编码 = value
End Set
End Property
Public Property 营业所编码() As String
Get
Return g营业所编码
End Get
Set(ByVal value As String)
g营业所编码 = value
End Set
End Property
Protected Overrides Function CreateDataAdapter() As System.Data.OleDb.OleDbDataAdapter
Return Database.Instance.GetDataAdapter(Constant.GetSelect(TableType.用户), True)
End Function
Protected Overrides Function CreateTable() As System.Data.DataTable
Return New BankPayDataSet.用户DataTable
End Function
Protected Overrides Function SetName() As String
Return Constant.TableType.用户.ToString
End Function
End Class
[解决办法]
接
[解决办法]
ddddddddddddddddd
[解决办法]
mark!
[解决办法]
jf 楼主加油吧 呵呵
[解决办法]
请问楼主是哪个城市的自来水公司的,我也是自来水公司的,我前段时间帮我公司开发了一套管网阀门管理信息系统,有空多多交流!