visual basic sql register现有用户检测 [英] visual basic sql register existing user detect

查看:83
本文介绍了visual basic sql register现有用户检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello I have a registration form mysql need to add code to detect if the user exists. The existence if it should show the window (alredy registered user)
code:







导入MySql.Data.MySqlClient

公共类Form2



'创建一个公共函数并设置你的MySQL连接。

公共函数mysqlconnection()作为MySqlConnection

'返回新连接。

返回新的MySqlConnection(Server = xxx;用户ID = xx;密码= xx;数据库= xx)

结束功能



'将mysqlconnection()的值作为MySQL连接传递给con。

Public con As MySqlConnection = MySqlConnection()



'声明变量字符串

公共sql字符串

公共结果字符串



'声明这些课程

Dim cmd As New MySqlCommand

Public dt As New DataT能够

Public da As New MySqlDataAdapter





Public Sub create(ByVal sql As String)



尝试

'打开连接

con.Open()

'设置你的命令

使用cmd

'保存要执行的数据

.Connection = con

.CommandText = sql

'执行数据

result = cmd.ExecuteNonQuery



'对结果进行编码,无论是成功还是失败时它被执行。

如果结果= 0那么

'数据的执行失败

MsgBox(注册失败!)

否则

'执行的数据是成功的

MsgBo x(你现在已注册。)

结束如果

结束

Catch ex As Exception

MsgBox(ex.Message)

结束尝试

con.Close()

结束子





Private Sub btnsave_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnsave.Click





'将字符串值放到sql

sql =INSERT INTO`user`(`email`,`username`,`Pass`)VALUES('_

& txtemail.Text& ','& txtuser.Text& ','& txtpass.Text& ')



'调用你的子名并将sql放在参数列表中。

create(sql)



结束次级











Private Sub Form2_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load





End Sub



Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)



结束子

结束类




Imports MySql.Data.MySqlClient
Public Class Form2

'create a public function and set your MySQL Connection .
Public Function mysqlconnection() As MySqlConnection
'return new connection.
Return New MySqlConnection("Server=xxx;User ID=xx;Password=xx;Database=xx")
End Function

'pass the value of mysqlconnection() as MySQL connection to con.
Public con As MySqlConnection = MySqlConnection()

'declaring the variables string
Public sql As String
Public result As String

'declaring the classes
Dim cmd As New MySqlCommand
Public dt As New DataTable
Public da As New MySqlDataAdapter


Public Sub create(ByVal sql As String)

Try
'open the connection
con.Open()
'set your command
With cmd
'holds the data to be executed
.Connection = con
.CommandText = sql
'execute the data
result = cmd.ExecuteNonQuery

'coditioning the result whether succesfull or failed when it is executed.
If result = 0 Then
'the execution of data is failed
MsgBox("Registration failed!")
Else
'the executed data is succesfull
MsgBox("You are now registered.")
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub


Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click


'put the string value to sql
sql = "INSERT INTO `users` (`email`, `username`, `Pass` ) VALUES ('" _
& txtemail.Text & "','" & txtuser.Text & "','" & txtpass.Text & "')"

'call your sub name and put the sql in the parameters list.
create(sql)

End Sub





Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub
End Class

推荐答案

在插入之前,请尝试检查重复的用户名。 />
Try this to check duplicate username before inserting.
sqlcommand = New SqlCommand("Select * from users where username=yourusername", cnn)
Dim adp As SqlDataAdapter = New SqlDataAdapter(sqlcommand)
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
Return True
End If
Return False


这篇关于visual basic sql register现有用户检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆