2012-06-25 45 views
0

我仍然使用Asp.net用vb.net如何從數據庫添加會話?

Protected Sub login_Click(ByVal sender As Object, ByVal e As EventArgs) Handles login.Click 
     Dim querystrings As String = "Select Email, Password ,Roles, Nama_Depan, Nama_Belakang from Employee where Email = @email;" 
     Dim con As New SqlConnection(connected) 
     Dim cmd As New SqlCommand(querystrings, con) 
     Dim myreader As SqlDataReader 

    Session ("Namadepan")= cmd.Parameters("Nama_Depan") 

end sub 

我想存儲的角色,Nama_Depan,Nama_Belakang與會話新的,但我不知道

回答

1

做處理這個邏輯,所以Session()Session.Add()未散你整個網站的一類。

Public Shared ReadOnly Property Instance() As ClassStoredInSession 
    Get 
     'store the object in session if it is not already stored 
     If Session("example") Is Nothing Then 
      Session("example") = New ClassStoredInSession() 
     End If 

     'return the object stored in session 
     Return DirectCast(Session("example"), ClassStoredInSession) 
    End Get 
End Property 
0

如何使一個類即UserSession它有它的屬性作爲從數據庫中檢索到的數據,並將該類的實例插入到Session對象中。

僞代碼:

UserSession user = new UserSession(name , roles ....) 
Session["userData"] = user; 
0

參見本:

Session("Namadepan") = cmd.Parameters["Nama_Depan"].Value 
0

您需要獲取您SqlDataReader的數據結果,並將其存儲在會議:Save values in Session

,因爲它是那麼簡單。

對您的服務器執行sql命令後,myReader應包含您需要的數據。 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader(v=vs.71

然後,您可以從讀取器讀取數據並將其存儲在會話中。您可以製作一個特定的「UserData」對象並將其存儲在會話中。

會議( 「的UserData」)=新的UserData {}

0

會議( 「用戶名」)= myreader( 「Nama_Depan」)

+0

這是您的解決方案? – adatapost