如何在将数据提交到数据库之前停止并重置表单

时间:2015-12-17 21:00:35

标签: vb.net

我正在创建一个登录页面,我已经完成了注册表单,但是,我需要验证用户名。我已经完成了验证部分,但是,我似乎无法让它不提交数据并重置用户名框。这是代码

Imports System.Data.OleDb

公共类注册

Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection

Private Sub rB_Click(sender As Object, e As EventArgs) Handles rB.Click
    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    dataFile = "C:\Users\Harry\Documents\Visual Studio 2015/users.accdb"
    connString = provider & dataFile
    myConnection.ConnectionString = connString

    myConnection.Open()
    Dim usf As OleDbCommand = New OleDbCommand("SELECT * FROM [users] WHERE [username] = '" & uT.Text, myConnection)
    Dim userFound As Boolean = True
    If userFound = True Then
        MsgBox("Username already found; Please choose another")
        Dim frm = New Register
        frm.Show()
        Me.Close()
    End If

    Dim str As String
    str = "insert into users ([username], [password], [Firstname], [LastName]) values (?, ?, ?, ?)"
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    cmd.Parameters.Add(New OleDbParameter("username", CType(uT.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("password", CType(pT.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("FirstName", CType(fnT.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("LastName", CType(lnT.Text, String)))
    Try
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        myConnection.Close()
        uT.Clear()
        pT.Clear()
        fnT.Clear()
        lnT.Clear()
        Me.Hide()
        Form2.Show()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

首先,请发布您的代码,而不是包含图片,因为在描述发现的问题时很难引用。

话虽如此。你有一个布尔的UserFound,你声明并设置为True,然后立即检查它是否为真。当然,这是真的,你只需设置它。

此外,我在那张图片中看不到你甚至传递查询以获得结果。 (即executeReader,executeScalar)

通常,您可以查询数据库,然后检查是否返回了任何行,或者对单个列使用executeScalar以查看是否返回了值。