在创建帐户时查看记录是否已存在

时间:2014-11-03 12:08:12

标签: vb.net dbnull

您好我正在尝试为我的访问数据库创建一个帐户系统,我遇到了错误"运营商'>'未定义类型' DBNull'并输入'整数'。"我已经搜索了我的问题的修复程序,但无法找到任何有效或有帮助的答案?我对编码很陌生,所以感谢任何有用的建议

Dim conn As New OleDbConnection
Dim myqry As String = Nothing
Dim mycmd As New OleDbCommand 



mycmd.Connection = conn
    mycmd.CommandText = "SELECT studentUser from tblStudents;"
    Dim dr As OleDbDataReader = mycmd.ExecuteReader
    Dim value As Boolean = False
    While dr.Read

        If dr(0) > 0 Then
            value = True
        End If
    End While

    If value = True Then
        MsgBox("Username Already Exists, Please enter a new one")


    Else

        Dim sqlQry As String

        sqlQry = "INSERT INTO tblStudents(studentName, TutorGroup, studUser, studPass) VALUES(student_Name, student_Group, student_Username , student_Password)"

        Dim cmd As New OleDbCommand(sqlQry, conn)
        cmd.ExecuteNonQuery()

        MsgBox("Your account has successfully been created")
        Me.Hide()
        Login_Student.Show()
    End If  

1 个答案:

答案 0 :(得分:0)

您需要对帐户设置约束,例如,学生名称唯一。

然后用Try ... Catch:

包装你的插入调用
sqlQry = "INSERT INTO tblStudents(studentName, TutorGroup, studUser, studPass) VALUES(student_Name, student_Group, student_Username , student_Password)"
Dim cmd As New OleDbCommand(sqlQry, conn)
Try
  cmd.ExecuteNonQuery()
Catch ex As ConstraintException
  MsgBox("Username Already Exists, Please enter a new one")
End Try

首先执行读取,然后写入将不能用于并发,因为另一个用户可能在您的支票和实际写入之间编写帐户。