【发布时间】:2009-11-02 18:52:25
【问题描述】:
我不明白为什么在 .net 紧凑型框架中向插入函数发送参数运行缓慢。
例如以下代码在 2 秒内插入
cn = New SqlCeConnection(strstring)
cmd = New SqlCeCommand
Dim rs As SqlCeResultSet
cmd.Connection = cn
cmd.CommandType = CommandType.TableDirect
cn.Open()
Dim rec As SqlCeUpdatableRecord
Dim DB As New Db
Dim a As Integer = 1
Dim b As Integer = 2
For i As Integer = 0 To 1000
If i = 0 Then
cmd.CommandText = "A"
rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable)
rec = rs.CreateRecord()
End If
Try
rec.SetValue(0, a)
rec.SetValue(1, b)
rs.Insert(rec
Catch ex As Exception
End Try
Next
但是当我将参数 a 和 b 发送到插入子时,这段代码在 13 秒内完成。性能下降了什么?
cn = New SqlCeConnection(strstring)
cmd = New SqlCeCommand
Dim rs As SqlCeResultSet
cmd.Connection = cn
cmd.CommandType = CommandType.TableDirect
cn.Open()
Dim rec As SqlCeUpdatableRecord
For i As Integer = 0 To 1000
If i = 0 Then
cmd.CommandText = "A"
cmd.CommandType = CommandType.TableDirect
rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable)
rec = rs.CreateRecord()
End If
Try
DB.Insert(1, 2, rs, rec)
Catch ex As Exception
End Try
Next
这是在 db 类中插入 sub
公共类数据库
Public Shared Sub Insert(ByVal a As Integer, ByVal b As Integer, ByRef rs As SqlCeResultSet, ByRef rec As SqlCeUpdatableRecord)
Try
rec.SetValue(0, a)
rec.SetValue(1, If(b = String.Empty, DirectCast(DBNull.Value, Object), b))
rs.Insert(rec)
Catch ex As Exception
End Try
End Sub
结束课程
【问题讨论】:
-
2 个整数拷贝到堆栈?
-
拿出分析器看看?
-
你检查过其中一个没有抛出异常吗?
-
没有例外,使用 try catch 不会减少。我猜堆栈的 2 个整数会变慢,我该如何防止呢?当我将整数视为空时,它不会改变。
-
在代码中使用空的 try/catch 是一个非常糟糕的主意。您可以删除它们并再次测试吗?
标签: .net vb.net exception-handling