[英]VB.net and Mysql Login Problem Using Two Tables
How can i login using doctor level in his table.Because everytime i log the receptionist form is always popping even when i log using the user and pass of the doctor maybe my syntax is wrong please help me PS: Doctors Forms and Receptionists Forms are different.我如何在他的表中使用医生级别登录。因为每次我登录时接待员表格总是弹出,即使我使用用户登录并通过医生可能是我的语法错误请帮帮我 PS:医生表格和接待员表格是不同的.
This is my code:这是我的代码:
Try
mycon.Open()
Dim query1 As String
Dim query2 As String
Dim name As String
query1 = "SELECT * FROM receptionisttbl"
command = New MySqlCommand(query1, mycon)
reader = command.ExecuteReader
With reader
.Read()
If .Item("level") = "1" Then
With reader
.Read()
name = .Item("name")
End With
MsgBox("Welcome " + name)
Form1.Show()
command.Dispose()
reader.Close()
mycon.Close()
Else
mycon.Open()
query2 = "SELECT * FROM doctortbl"
command = New MySqlCommand(query2, mycon)
reader = command.ExecuteReader
With reader
.Read()
If .Item("level") = "2" Then
With reader
.Read()
name = .Item("name")
End With
MsgBox("Welcome " + name)
Form2.Show()
command.Dispose()
reader.Close()
mycon.Close()
End If
End With
End If
End With
Catch ex As Exception
MsgBox(ex.ToString)
Finally
mycon.Dispose()
End Try
First, let's consider your requirements.首先,让我们考虑一下您的要求。
Provide a login form to validate users.提供登录表单以验证用户。
If the user is valid (userName and matching password are found in the database) then display a Form based on the level of the user.如果用户有效(在数据库中找到了用户名和匹配的密码),则根据用户的级别显示一个表单。
Considering the requirements look at the design of the table in the database.考虑需求,查看数据库中表的设计。 Let's call it MDUsers.我们称之为 MDUsers。 Now to the columns.现在到列。
UserName as an VarChar size 100 and Primary key of the table
UserPassword as VarChar size 100
Level an Integer
Now you have fields for all the data you need for your requirements.现在,您拥有满足您的要求所需的所有数据的字段。
Add some sample data to your table.将一些示例数据添加到您的表中。
Next think about what you need from the user to accomplish your requirements.接下来想想你需要从用户那里得到什么来完成你的要求。 Design your user interface accordingly.相应地设计您的用户界面。 Your login form will have 2 text boxes (userName and password) and a button (Login).您的登录表单将有 2 个文本框(用户名和密码)和一个按钮(登录)。
Then you need to think about the code for the Login button.然后您需要考虑登录按钮的代码。 You need to connect to your database and check if there is a match to userName and password.您需要连接到您的数据库并检查用户名和密码是否匹配。 Once you find a match you only need to retrieve a single piece of data to complete your requirements, the level.一旦找到匹配项,您只需检索单个数据即可完成您的要求,即级别。
Now how can we ask the database to do all that?现在我们怎样才能让数据库做这一切呢?
Select level From MDUsers Where userName = @userName And UserPassword = @pword;
Since userName is the Primary Key of the table we know we will find no more than one record with that userName.由于 userName 是表的主键,我们知道我们不会找到超过一条具有该 userName 的记录。 If the password matches too then we have a valid user and we will return the single piece of data, level for that record.如果密码也匹配,那么我们有一个有效的用户,我们将返回单个数据,该记录的级别。
Using...End Using
blocks ensure that your database objects are closed and disposed even if there is an error. Using...End Using
块确保您的数据库对象即使出现错误也已关闭和处理。 You can pass your connection string directly to the constructor of the connection and pass the query string and connection to the constructor of the command.您可以将连接字符串直接传递给连接的构造函数,并将查询字符串和连接传递给命令的构造函数。
Always use parameters.始终使用参数。
Since we only need a single piece of data we can use .ExecuteScalar由于我们只需要一条数据,我们可以使用 .ExecuteScalar
Private Sub OpCode()
Dim retVal As Object
Using cn As New MySqlConnection("Your connection string"),
cmd As New MySqlCommand("Select level From MDUsers Where userName = @userName And UserPassword = @pword;", cn)
cmd.Parameters.Add("@userName", MySqlDbType.VarChar, 100).Value = txtName.Text
cmd.Parameters.Add("@pword", MySqlDbType.VarChar, 100).Value = txtPassword.Text
cn.Open()
retVal = cmd.ExecuteScalar
End Using
If retVal Is Nothing Then
MessageBox.Show("Login failed")
Return
End If
If CInt(retVal) = 1 Then
Form1.Show()
ElseIf CInt(retVal) = 2 Then
Form2.Show()
Else
MessageBox.Show("Level not recognized")
End If
End Sub
You will have to check MySql rules for column names and table names being aware of reserved words.您必须检查 MySql 规则以了解保留字的列名和表名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.