我是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
答案 0 :(得分:2)
LINQ to SQL在下面使用ADO.NET,因此它无法更快。
你可以问,为什么人们使用它,什么时候性能更差?因为在某些情况下,性能并不是那么重要。你必须管理,LINQ to SQL更容易使用!因为开发过程可以更快,更容易,更友好地开展。