帮助vb.net代码,该代码不允许某些用户进入系统 [英] help vb.net code that does not allow some users to enter the system
问题描述
我在我的项目上使用vb.net,并且我需要有关(不允许某些用户进入系统的代码)的帮助
我有一个登录表单,其中包含(2个用于输入用户名的文本框和用于登录系统的登录+按钮)
我也有用于登录的数据库表,其中有(用户名+密码+ user_type + record_status)
问题是((((如果用户被管理员删除,则他无法进入系统)))
但是我该怎么做呢?
她是我的代码:
尝试
Dim dt As 字符串
dt =现在
icount = "
Mycn = 新建 SqlConnection(" )
Mycn.Open()
' SQLstr =从登录名中选择Userlevel,其中user_name ='"& txtname.Text.Trim& 'and password ='"& txtpass.Text.Trim& "
SQLstr = " & txtname.Text.Trim& " & txtpass.Text.Trim& "
命令= 新建 SqlCommand(SQLstr,Mycn)
icount = command.ExecuteScalar
如果 icount = " 然后
My.Computer.Audio.Play(My.Resources.Windows_Error,AudioPlayMode.Background)
MessageBox.Show(" ," 登录...",MessageBoxButtons.OK,MessageBoxIcon.Information)
其他
usrID = txtname.Text.Trim
My.Computer.Audio.Play(My.Resources.car,AudioPlayMode.Background)
Timer1.Start()
Timer2.Start()
结束 如果
如果 icount.Trim<> " 然后
mainpageadmin.SystemSettingToolStripMenuItem.Enabled = 错误
结束 如果
捕获,例如 As 异常
MessageBox.Show(例如Message& " )
Mycn.Close()
结束 尝试
如果 icount.Trim<> " 然后
MessageBox.Show(" ," ,MessageBoxButtons.OK,MessageBoxIcon.Information)
结束 如果
Mycn.Close()
好吧,你是一个初学者,所以...一步一步...
STEP 1)
要使用登录"表单构建应用程序,我们需要创建自定义类 [界面 [ ' 这是我们班级的界面 公共 界面 IMyApp 功能 CheckUser( ByVal sUName As 字符串, ByVal sUPwd As As 属性 UserName() As 字符串 结束 界面
===类===
' 这是我们的类,它从接口中实现方法和属性 公共 类 TMyApp 实现 IMyApp ' 如何建立连接字符串? ' http://www.connectionstrings.com/ Const sConn As 字符串 = " Dim oConn As SqlClient.SqlConnection = 没什么 Dim sUserName As 字符串 = 字符串.空 公共 子 新建() ' 在这里您可以启动一些属性... 结束 子 公共 功能 CheckUser( ByVal sUName As 字符串, ByVal sUPwd 字符串)整数 实现 IMyApp.CheckUser Dim oComm As SqlClient.SqlCommand = 没什么 Dim oRdr As SqlClient.SqlDataReader = 没什么 Dim oDt As Data.DataTable = 没什么 Dim sSQL As String = 字符串.空 Dim iRetVal As 整数 = 0 尝试 oConn = 新建 SqlClient.SqlConnection(sConn) oConn.Open() sSQL = " & vbCr& _ " & vbCr& _ " oComm = 新建 SqlClient.SqlCommand(sSQL,oConn) 使用 oComm .CommandType = CommandType.Text .Parameters.AddWithValue(" ,sUName) .Parameters.AddWithValue(" ,sUPwd) oRdr = .ExecuteReader() 结束 使用 oDt = 新建 Data.DataTable() oDt.Load(oRdr) iRetVal = oDt.Rows.Count 选择 案例 iRetVal 案例 0 ' 未知用户或用户不存在 MsgBox(" ,MsgBoxStyle.Information," ) 退出 尝试 案例 1 ' 是的,欢迎 sUserName = sUName 案例 其他 MsgBox(" " 信息...") ' 超过1位用户,有可能吗? 结束 选择 捕获,例如 As Data.DataException iRetVal = -1 MsgBox(例如消息,MsgBoxStyle.Exclamation," ) 捕获,例如 As SqlClient.SqlException iRetVal = -1 MsgBox(例如消息,MsgBoxStyle.Exclamation," ) 捕获,例如 As 异常 iRetVal = -1 MsgBox(例如消息,MsgBoxStyle.Exclamation," ) 最后 oDt = 没什么 oRdr.Close() oRdr = 什么都没有 oComm.Dispose() oComm = 没什么 如果 oConn.State = ConnectionState.打开然后 oConn.Close() oConn = 没什么 结束 尝试 返回 iRetVal 结束 功能 公共 属性 UserName() As 字符串 实现 IMyApp.UserName 获取 返回 sUserName 结束 获取 设置( ByVal 值 As 字符串) sUserName =值 结束 设置 结束 属性 受保护的 替代 Sub Finalize() MyBase .Finalize() 结束 子 结束 类
STEP 2)
现在,我们需要将我们的类声明为全局变量.为什么?要使其"可见 [ ===模块===
模块 MainMod ' 这是一个可以存储全局变量的地方 ' 看看该过程:MainFrm.New() 公共 oMyApp As IMyApp = 什么都没有 结束 模块
STEP 3
登录时间
===登录表单类===
公共 类 LoginFrm 私有 子 OK_Click( ByVal 发件人 As 系统.对象, ByVal e As System.EventArgs)句柄确定.单击 Dim iCount As 整数 = 0 如果 我 .UsernameTextBox.Text.Length = 0 或 _ 我 .PasswordTextBox.Text.Length = 0 >然后跨度> MsgBox(" ,MsgBoxStyle.Information, 错误") 退出 子 结束 如果 iCount = MainMod.oMyApp.CheckUser(我 .UsernameTextBox.Text, Me .PasswordTextBox.Text) 选择 案例 iCount = 1 案例 0 ' 未知用户 我 .OK.DialogResult = Windows.Forms.DialogResult.Abort 案例 1 ' 确定 我 .OK.DialogResult = Windows.Forms.DialogResult.OK 案例 其他 ' 多个用户? 我 .OK.DialogResult = Windows.Forms.DialogResult.Cancel 结束 选择 我 .Close() 结束 子 私有 子 Cancel_Click( ByVal 发件人 As 系统.对象, ByVal e As System.EventArgs)句柄取消.单击 我 .Close() 结束 子 私有 子 LoginFrm_Load( ByVal 发件人 As 系统.对象, ByVal e As System.EventArgs)句柄 MyBase .Load 我 .UsernameTextBox.Text = Environ(" ) 结束 子 结束 类
STEP 4
===主表单类===
公共 类 MainFrm 公共 子 新建() Dim dlgRes As DialogResult = Windows.Forms.DialogResult.OK ' Windows窗体设计器需要此调用. InitializeComponent() ' 在InitializeComponent()调用之后添加任何初始化. ' 通过界面启动我们的课程,看看MainMod.vb oMyApp = 新 TMyApp ' 循环调用LoginFrm 做 dlgRes = LoginFrm.ShowDialog() ' 如果用户取消登录,则跳过初始化初始化 如果 dlgRes = Windows.Forms.DialogResult.Cancel 然后 退出 子 ' 欢迎;) 如果 dlgRes = Windows.Forms.DialogResult.OK And oMyApp.UserName< 字符串.空然后 退出 执行 ' 未知用户-看一下LoginFrm.vb 循环,直到dlgRes = Windows.Forms.DialogResult.Abort ' 简单的初始化:) 我 .LblInfo.Text = " & oMyApp.UserName& " 结束 子 受保护的 替代 Sub Finalize() oMyApp = 没什么 MyBase .Finalize() 结束 子 结束 类
也许这不是最佳编码(尤其是登录的一部分),所以我说:对不起.
最重要的想法是:显示简单有效的示例.
希望对您有所帮助;)
hi
i am using vb.net on my project and i want help on the (code that does not allow some users to enter the system)
i have a form for login which contain( 2 textboxes for username and login + button for sign in to the system)
also i have database table for login which have (username+ password + user_type + record_status)
the problem is (((if the user is deletet by admin then he can not enter the system)))
but how can i do this succsesfuly??
her is my code:
Try
Dim dt As String
dt = Now
icount = ""
Mycn = New SqlConnection("Data Source=IMAN-PC;Initial Catalog=control car;Integrated Security=True")
Mycn.Open()
'SQLstr = "select Userlevel from login where user_name='" & txtname.Text.Trim & "' and password='" & txtpass.Text.Trim & "' "
SQLstr = "select Userlevel , record_statuse from login where user_name='" & txtname.Text.Trim & "' and password='" & txtpass.Text.Trim & "'"
command = New SqlCommand(SQLstr, Mycn)
icount = command.ExecuteScalar
If icount = "" Then
My.Computer.Audio.Play(My.Resources.Windows_Error, AudioPlayMode.Background)
MessageBox.Show("Invalid UserName or Password", "Login...", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
usrID = txtname.Text.Trim
My.Computer.Audio.Play(My.Resources.car, AudioPlayMode.Background)
Timer1.Start()
Timer2.Start()
End If
If icount.Trim <> "Admin" Then
mainpageadmin.SystemSettingToolStripMenuItem.Enabled = False
End If
Catch ex As Exception
MessageBox.Show(ex.Message & "Could NOT Login")
Mycn.Close()
End Try
If icount.Trim <> "A" Then
MessageBox.Show("You can not enter the system because you are deleted by ADMINISTRATOR", "Login...", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Mycn.Close()
OK, you''re a beginner, so... step by step...
STEP 1)
To build an application with Login form, we need to create custom class[^] and interface[^] to communicate between application and our class.
Before you start programming, please, read carefully articles (links above) about interfaces and classes.
=== interface ===
'This is an interface for our class Public Interface IMyApp Function CheckUser(ByVal sUName As String, ByVal sUPwd As String) As Integer Property UserName() As String End Interface
=== class ===
'This is our class, which implements methods and properties from our interface Public Class TMyApp Implements IMyApp 'how to build connection string? 'http://www.connectionstrings.com/ Const sConn As String = "Data Source=YOUR_SQL_SERVER;Initial Catalog=YOUR_DATABASE;Integrated Security=True" Dim oConn As SqlClient.SqlConnection = Nothing Dim sUserName As String = String.Empty Public Sub New() 'here you can initiate some properties... End Sub Public Function CheckUser(ByVal sUName As String, ByVal sUPwd As String) As Integer Implements IMyApp.CheckUser Dim oComm As SqlClient.SqlCommand = Nothing Dim oRdr As SqlClient.SqlDataReader = Nothing Dim oDt As Data.DataTable = Nothing Dim sSQL As String = String.Empty Dim iRetVal As Integer = 0 Try oConn = New SqlClient.SqlConnection(sConn) oConn.Open() sSQL = "SELECT * " & vbCr & _ "FROM [YOUR_DATABASE].[dbo].[TABLE]" & vbCr & _ "WHERE login=@UName AND pwd=@UPwd" oComm = New SqlClient.SqlCommand(sSQL, oConn) With oComm .CommandType = CommandType.Text .Parameters.AddWithValue("@UName", sUName) .Parameters.AddWithValue("@UPwd", sUPwd) oRdr = .ExecuteReader() End With oDt = New Data.DataTable() oDt.Load(oRdr) iRetVal = oDt.Rows.Count Select Case iRetVal Case 0 'unknown user or user doesn't exist MsgBox("Unknow user or user doesn't exists!", MsgBoxStyle.Information, "Information...") Exit Try Case 1 'YES, welcome sUserName = sUName Case Else MsgBox("More than one user???", MsgBoxStyle.Information, "Information...") 'more than 1 user, is it possible??? End Select Catch ex As Data.DataException iRetVal = -1 MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...") Catch ex As SqlClient.SqlException iRetVal = -1 MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...") Catch ex As Exception iRetVal = -1 MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...") Finally oDt = Nothing oRdr.Close() oRdr = Nothing oComm.Dispose() oComm = Nothing If oConn.State = ConnectionState.Open Then oConn.Close() oConn = Nothing End Try Return iRetVal End Function Public Property UserName() As String Implements IMyApp.UserName Get Return sUserName End Get Set(ByVal value As String) sUserName = value End Set End Property Protected Overrides Sub Finalize() MyBase.Finalize() End Sub End Class
STEP 2)
Now, we need to declare our class as a global variable. Why? To make it "visible[^]" in all forms (we need a module).
=== module ===
Module MainMod 'this is a place where you can store global variables 'take a look into the procedure: MainFrm.New() Public oMyApp As IMyApp = Nothing End Module
STEP 3
Time for Login form
=== login form class ===
Public Class LoginFrm Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click Dim iCount As Integer = 0 If Me.UsernameTextBox.Text.Length = 0 Or _ Me.PasswordTextBox.Text.Length = 0 Then MsgBox("You need to fill Login and Password", MsgBoxStyle.Information, "Error") Exit Sub End If iCount = MainMod.oMyApp.CheckUser(Me.UsernameTextBox.Text, Me.PasswordTextBox.Text) Select Case iCount = 1 Case 0 'unknown user Me.OK.DialogResult = Windows.Forms.DialogResult.Abort Case 1 'OK Me.OK.DialogResult = Windows.Forms.DialogResult.OK Case Else 'more than one user??? Me.OK.DialogResult = Windows.Forms.DialogResult.Cancel End Select Me.Close() End Sub Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click Me.Close() End Sub Private Sub LoginFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.UsernameTextBox.Text = Environ("username") End Sub End Class
STEP 4
=== Main form class ===
Public Class MainFrm Public Sub New() Dim dlgRes As DialogResult = Windows.Forms.DialogResult.OK ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. 'initiate our class through an interface, take a look into MainMod.vb oMyApp = New TMyApp 'call LoginFrm in a loop Do dlgRes = LoginFrm.ShowDialog() 'if user cancel login, skip initialization of apllication If dlgRes = Windows.Forms.DialogResult.Cancel Then Exit Sub 'welcome ;) If dlgRes = Windows.Forms.DialogResult.OK And oMyApp.UserName <> String.Empty Then Exit Do 'unknown user - take a look into LoginFrm.vb Loop Until dlgRes = Windows.Forms.DialogResult.Abort 'Simple initialization :) Me.LblInfo.Text = "Welcome " & oMyApp.UserName & "!" End Sub Protected Overrides Sub Finalize() oMyApp = Nothing MyBase.Finalize() End Sub End Class
Maybe this is not optimal coding (especially the part of login), so i say: sorry.
The most important idea was: to show a simple, working example.
I hope it will be helpful ;)
这篇关于帮助vb.net代码,该代码不允许某些用户进入系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!