VB.NET LOGIN PAGE既問題

2008-04-11 8:18 am
我已經成功做到login(即係link到個mdb)
但係點樣先可以做到 login Successful之後個form彈去另個一個form?
如果fail就轉唔到
以下係我打既code

Dim strCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Staff_Info.mdb;Persist Security Info=False"

Dim conn As New OleDbConnection(strCon)

Try
Dim dr As OleDbDataReader
Dim SID As String = txtStaffID.Text
Dim Pwd As String = txtPwd.Text

Dim strSQL As String = "Select * from Staff_Info where Staff_ID= '" & SID & "' and Staff_Pwd= '" & Pwd & "'"
Dim cmdLogin As New OleDbCommand(strSQL, conn)

conn.Open()
dr = cmdLogin.ExecuteReader

If dr.Read Then
lblMsg.Text = "Successful Login!"

Else

lblMsg.Text = "Login Failed!"

End If
conn.Close()

Catch ex As Exception
lblMsg.Text = ex.Message

End Try

回答 (3)

2008-04-12 11:15 am
✔ 最佳答案
你用 Windows Form 還是 ASP.NET﹖

2008-04-12 03:15:16 補充:
Dim success As Boolean = False ' 加一個變數
If dr.Read Then
lblMsg.Text = "Successful Login!" ' 這行無用可以不要
success = True
Else
lblMsg.Text = "Login Failed!"
End If
dr.Close() ' 記得記得要 close DataReader
conn.Close()

If success Then
Dim frm2 As New Form2()
frm2.Show()
Me.Close() ' 登入成功還要登入視窗幹麽
End If
2008-04-11 4:50 pm
windows form
2008-04-11 10:49 am
Login success 後, 想 PopUp 另一張 form 可用 .ShowDialog method.

假設另一張form既 Name 係 frmDoSth, 則可係
lblMsg.Text = "Successful Login!" 之後加多行
frmDoSth.ShowDialog

見議:
你可將 Login 既流程
(彈 login 張form, connect db, sql query, display msg, etc.)
寫成一個Function (e.g. function LoginOK() as boolean),
如果 login success 傅回 true 否則傅回 false.

係 startup form 既 FormLoad event call 呢個 function.
跟據傅回值再判斷程式應繼續或離開. 像:

sub frmMain.FormLoad handle frmMain_FormLoad
if not LoginOK then
Application.Exit
end if

' 係呢度繼績成功 Login 後既 code

end sub

如 syntax 有錯, 請自行更正.
HopeThisHelp


收錄日期: 2021-04-25 20:33:48
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080411000051KK00041

檢視 Wayback Machine 備份