1

我是 LINQ 的新手,所以为了开始探索,我创建了一个多线程时间应用程序来查看更快的 LINQ 或 ADO。这个应用程序计算我在回家之前工作了多长时间。定时器设置为 1 秒,调用同一个 SP。

我错过了什么吗?

Imports System.Data.SqlClient
Imports System.Threading

Public Class Time

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    lblADOTime.Text = ADO()
    lblTime.Text = Net()
    lblLinq.Text = tLinq()

End Sub

Private Sub Time_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim tADO As Thread
    Dim tNet As Thread
    Dim tLinq As Thread
    tADO = New Thread(AddressOf Me.ADO)
    tADO.Start()
    tNet = New Thread(AddressOf Me.Net)
    tNet.Start()
    tLinq = New Thread(AddressOf Me.tLinq)
    tLinq.Start()
End Sub
Private Function tLinq() As String
    Dim db As New DataClasses1DataContext
    'db.Connection.Open()

    Dim myDate As String
    myDate = Date.Today + " 17:00:00"
    Return "LINQ: " + db.TimeLeftToday(myDate).ToList().Last.Column1

    'db.Connection.Close()

End Function
Private Function Net() As String
    Dim a As DateTime
    a = Date.Today + " 17:00:00"
    Dim s As String
    s = (a - Now()).ToString
    Return ".NET: " + s 's.Substring(1, 8)

End Function

Private Function ADO() As String
    Try
        Dim conn As New SqlConnection()
        conn.ConnectionString = meh

        Dim cmd As New SqlCommand("TimeLeftToday", conn)

        cmd.CommandText = "TimeLeftToday"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@Today", SqlDbType.DateTime, 0, "@Today"))
        cmd.Parameters(0).Value = Date.Today + " 17:00:00"
        cmd.Connection = conn
        conn.Open()
        Dim sdr As SqlDataReader = cmd.ExecuteReader()
        sdr.Read()
        If sdr.HasRows Then

            Return "ADO: " + sdr.GetValue(0)

        End If
        conn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)



    End Try

End Function

End Class
4

1 回答 1

2

LINQ to SQL 在下面使用 ADO.NET,所以它不可能更快。

您可能会问,为什么人们使用它,当它的性能更差时?因为在某些情况下,性能并不是那么重要。而且你必须管理,LINQ to SQL 更容易使用!并且正因为如此,开发过程可以更快、更容易并且以对开发人员更友好的方式完成。

于 2013-03-07T19:46:24.527 回答