
一、制作如下界面:
二、编写代码:
Imports System.Web.Security '加密所需类库
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Data
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection ' 'OleDbConnection1 ' Me.OleDbConnection1.ConnectionString = "Provider=Microsoft Jet OLEDB 4.0;Data Source=dbs.mdb" End Sub Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents TBName As System.Web.UI.WebControls.TextBox Protected WithEvents TBPwd As System.Web.UI.WebControls.TextBox Protected WithEvents Result As System.Web.UI.WebControls.Label Protected WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection '注意: 以下占位符声明是 Web 窗体设计器所必需的。 '不要删除或移动它。 Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 '从WebConfig文件中读入数据库连接字符串 Dim connstr As String = ConfigurationSettings.AppSettings("DBConn") 'Dim OleDbConnection1 As New OleDbConnection(ConfigurationSettings.AppSettings("DBConn")) OleDbConnection1.ConnectionString = ConfigurationSettings.AppSettings("DBConn") & System.Web.HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings("dbPath")) + ";" 'Me.Result.Text = Me.OleDbConnection1.ConnectionString If OleDbConnection1.State = ConnectionState.Closed Then OleDbConnection1.Open() End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strSQL, strPWD As String strSQL = "Select adname,adpsw From admin where (adname=" & "'" & TBName.Text & "'" strPWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(TBPwd.Text, "MD5") strSQL = strSQL & " and adpsw= '" & strPWD & "')" Dim cmd As New OleDbCommand(strSQL, OleDbConnection1) Dim i As Integer i = cmd.ExecuteNonQuery() If i = 0 Then Session("name") = Me.TBName.Text Session("IsPassed") = "true" Response.Write(Session("name") & "成功登陆") Else Result.Text = "验证失败!请重新登录!" End If Result.Text = strSQL End Sub End Class 三、WebConfig 文件 四、运行结果:
