2010-04-14

使用LINQ 得到Server時間

看來此需求是寫Winform的人比較會用的到
因為如果是用ASP.net直接使用Now就會得到Server的時間

廢話不多說,直接看Code
請先建一個新的檔案!檔名隨便取XXXXX.vb
內容如下:


Imports System
Imports System.Data.Linq.Mapping
Imports System.Reflection

Partial Class 你的LINQ.dbml檔案取的名稱DataClassesDataContext
Inherits System.Data.Linq.DataContext
'REF: http://bit.ly/9l6LDz

<[Function](Name:="GetDate", IsComposable:=True)> _
Public Function GetDate() As DateTime
Dim mi As MethodInfo = TryCast(MethodBase.GetCurrentMethod(), MethodInfo)
Return DirectCast(Me.ExecuteMethodCall(Me, mi, New Object() {}).ReturnValue, DateTime)
End Function
End Class



用法超級簡單
直接用Label1.Text = db.GetDate()就是Server的時間囉!

如果是寫C#的朋友請看
http://peteohanlon.wordpress.com/2008/01/11/sql-server-getdate-and-linq-to-sql/

0 comments: