Imports MySql.Data.MySqlClient Partial Class _Default(try...catch写法)

Imports MySql.Data.MySqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Private Shared ReadOnly cs As String = "Server=127.0.0.1;Database=mydata;User Id=root;Password=;Character Set=gbk"
    Private con As MySqlConnection = Nothing
    Private cmd As MySqlCommand = Nothing

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim b As MySqlConnectionStringBuilder = New MySqlConnectionStringBuilder()
        b.Server = "127.0.0.1"
        b.Database = "mydata"
        b.UserID = "root"
        b.Password = ""
        b.CharacterSet = "gbk"
        Response.Write(b.ConnectionString)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Request.Cookies("username") Is Nothing OrElse Request.Cookies("userpass") Is Nothing Then
                Return
            End If
            Dim username As String = HttpUtility.HtmlDecode(Request.Cookies("username").Value)
            Dim userpass As String = HttpUtility.HtmlDecode(Request.Cookies("userpass").Value)
            con = New MySqlConnection(_Default.cs)
            cmd = New MySqlCommand("select count(*) from t_user where username = ?username and userpass = ?userpass", con)
            cmd.Parameters.AddWithValue("username", username)
            cmd.Parameters.AddWithValue("userpass", userpass)
            con.Open()
            Dim i As Integer = cmd.ExecuteScalar()
            If i = 0 Then
                Return
            End If
            Response.Redirect("~/Index.aspx")
        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            cmd.Dispose()
            con.Dispose()
        End Try
    End Sub

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
        Try
            Dim username As String = Me.Login1.UserName.Trim().ToLower()
            Dim userpass As String = Me.Login1.Password.Trim().ToLower()
            Dim b As Boolean = Me.Login1.RememberMeSet
            con = New MySqlConnection(_Default.cs)
            cmd = New MySqlCommand("select count(*) from t_user where username = ?username and userpass = ?userpass", con)
            cmd.Parameters.AddWithValue("username", username)
            cmd.Parameters.AddWithValue("userpass", userpass)
            con.Open()
            Dim i As Integer = cmd.ExecuteScalar()
            If i = 0 Then
                Return
            End If
            Response.Cookies("username").Value = HttpUtility.UrlEncode(username)
            Response.Cookies("userpass").Value = HttpUtility.UrlEncode(userpass)
            If b Then
                Response.Cookies("username").Expires = DateTime.Now.AddDays(7) '设置cookie超时时间为7天
                Response.Cookies("userpass").Expires = DateTime.Now.AddDays(7)
            End If
            Response.Redirect("~/Index.aspx") '登录成功跳转
        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            cmd.Dispose()
            con.Dispose()
        End Try
    End Sub

End Class

你可能感兴趣的:(Default.aspx.vb)