建个模块, Dim varcnn As OleDb.OleDbConnection ReadOnly Property cnn() As OleDb.OleDbConnection
创新互联公司是一家专业提供益阳企业网站建设,专注与成都网站设计、成都做网站、html5、小程序制作等业务。10年已为益阳众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
Get
If varcnn Is Nothing OrElse varcnn.State ConnectionState.Open Then Dim cnstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""" My.Application.Info.DirectoryPath "\Contact.mdb"";Persist Security Info=True"
varcnn = New OleDbConnection(cnstr)
varcnn.Open()
End If
Return varcnn
End Get
登录窗体,新建一个就行了。代码Public Class LoginForm1
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Using cmd As New OleDb.OleDbCommand("select password from tbuser where userid =?", cnn)
cmd.Parameters.Add("?", OleDbType.VarChar).Value = Me.UsernameTextBox.Text
Dim obj As Object = cmd.ExecuteScalar
If (Not IsDBNull(obj)) AndAlso (obj IsNot Nothing) AndAlso obj = PasswordTextBox.Text Then
Me.DialogResult = Windows.Forms.DialogResult.OK
Else MessageBox.Show("密码不正确!") Return
End If
End Using Me.Close()
End Sub Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End SubEnd Class 项目属性里点查看应用程序事件,写代码Namespace My ' 以下事件可用于 MyApplication:
'
' Startup: 应用程序启动时在创建启动窗体之前引发。
' Shutdown: 在关闭所有应用程序窗体后引发。如果应用程序异常终止,则不会引发此事件。
' UnhandledException: 在应用程序遇到未处理的异常时引发。
' StartupNextInstance: 在启动单实例应用程序且应用程序已处于活动状态时引发。
' NetworkAvailabilityChanged: 在连接或断开网络连接时引发。
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
Dim f As New LoginForm1
If Not f.ShowDialog = DialogResult.OK Then
End
End If
End Sub End ClassEnd Namespace
你测试一下吧。
问题应该出在你的查询语句的结果没有值,你用你的查询语句到SQL中是否有返回值?记得加上你的条件,另外,你语句里这一句是多余的.Dim s As Object = sComm.ExecuteScalar 以下语句测试正常 Dim myCn As New SqlClient.SqlConnection Try myCn.ConnectionString = sConnString myCn.Open() Dim mySQL As String = "SELECT * FROM ygzl" Dim sComm As New SqlClient.SqlCommand(mySQL, myCn) If sComm.ExecuteScalar Then Dim User_MainForm As New Form1 User_MainForm.Show() Else MsgBox("用户名或密码不正确,请重试!") End If Catch ex As Exception MsgBox(ex.Message) Finally myCn.Close() '关闭连接 End Try
创建完相应的用户名,密码控件之后,输入以下代码:
Imports System.Data.OleDb
Public Class Form1
Private Sub cmdok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdok.Click
Dim m_username As String
Dim m_userpwd As String
Dim cmd_result As MsgBoxResult
Dim m_da As New OleDbDataAdapter
Dim m_ds As New DataSet
Dim m_command As New OleDbCommand
Dim strsql As String
m_username = txtusername.Text
m_userpwd = txtuserpwd.Text
strsql = " select * from user_info where username='" m_username "' and userpwd='" m_userpwd "'"
OleDbConnection1.Open()
m_command.CommandType = CommandType.Text
m_command.CommandText = strsql
m_command.Connection = OleDbConnection1
m_da.SelectCommand = m_command
m_da.Fill(m_ds, "user_info")
If m_ds.Tables("user_info").Rows.Count 0 Then
cmd_result = MsgBox("欢迎登录", MsgBoxStyle.OKOnly, "提示")
Else
MsgBox("登录失败,请确认用户名和密码")
End If
End Sub
End Class