添加用户到数据库不起作用

时间:2012-12-02 19:51:09

标签: asp.net vb.net

我是ASP.net的新手,我目前正在尝试在网站上创建注册页面。我成功地将用户添加到数据库但我决定在代码中添加另一个功能来检查哪些用户ID可用。例如,如果用户删除了他们的帐户,则他们的userID将再次可用。我正在尝试找到最小值和最大值,并根据它是最小值还是最大值来加1或减1。我可以运行我为此编写的代码没有错误,但用户没有添加到数据库中。任何人都可以帮我弄清楚我的代码中缺少什么来做这个吗?

编辑>>>>> 代码将用户添加到数据库,但它将新用户添加到-1。我似乎无法看到问题所在。

If (aDataReader2.Read() = False) Then
  aConnection1 = New OleDbConnection(aConnectionString)
  aConnection1.Open()
  aQuery = "Insert Into UserDetails "
  aQuery = aQuery & "Values ('" & userID & "','" & userFName & "','" & userLName & "','" & userEmail & "','" & userUsername & "','" & userPassword & "')"
  aCommand = New OleDbCommand(aQuery, aConnection1)
  aCommand.ExecuteNonQuery()
  aConnection1.Close()
ElseIf (min = 1) Then
  aConnection2 = New OleDbConnection(aConnectionString)
  aConnection2.Open()
  aCommand = New OleDbCommand(aQuery3, aConnection2)
  aDataReader2 = aCommand.ExecuteReader()
  userID = max + 1
  aQuery = "Insert Into UserDetails "
  aQuery = aQuery & "Values ('" & userID & "','" & userFName & "','" & userLName & "','" & userEmail & "','" & userUsername & "','" & userPassword & "')"
  aCommand = New OleDbCommand(aQuery, aConnection2)
  aCommand.ExecuteNonQuery()
  aConnection2.Close()
Else
  aConnection3 = New OleDbConnection(aConnectionString)
  aConnection3.Open()
  aCommand = New OleDbCommand(aQuery2, aConnection3)
  aDataReader2 = aCommand.ExecuteReader
  userID = min - 1
  aQuery = "Insert Into UserDetails "
  aQuery = aQuery & "Values ('" & userID & "','" & userFName & "','" & userLName & "','" & userEmail & "','" & userUsername & "','" & userPassword & "')"
  aCommand = New OleDbCommand(aQuery, aConnection3)
  aCommand.ExecuteNonQuery()
  aConnection3.Close()
  lblResults.Text = "User Account successfully created"
  btnCreateUser.Enabled = False
End If

这是我用来从数据库中获取最大值和最小值的代码。我得到的值都是0 - 当min应该是1而max应该是5

 Dim minID As Integer
 Dim maxID As Integer
 aQuery2 = "Select Min(UserID) AS '" & [minID] & "' From UserDetails"
 aQuery3 = "Select Max(UserID) AS ' " & [maxID] & "' From UserDetails"

1 个答案:

答案 0 :(得分:1)

很难说是什么 问题确实存在,因为我们只看到了它的一部分。 min和max来自哪里?

我无法为您提供解决方案,但是,我建议您更好地构建代码。你有很多(!)冗余代码。这使得代码难以阅读,理解,改变和测试。

将用户数据放入类中。这使得它比许多单个变量更容易处理。

Public Class User
    Public Property ID As Integer
    Public Property FirstName As String
    Public Property LastName As String
    Public Property EMail As String
    Public Property Username As String
    Public Property Password As String
End Class

将冗余代码提取到子程序

Private Sub CreateUser(ByVal u As User)
    Const InsertQuery As String = _
        "INSERT INTO UserDetails VALUES ({0},'{1}','{2}','{3}','{4}','{5}')"

    Dim query As String = String.Format(InsertQuery, u.ID, u.FirstName, u.LastName, _
                                                     u.Email, u.Username, u.Password)
    Using conn As New OleDbConnection(aConnectionString)
        conn.Open()
        Dim cmd As New OleDbCommand(query, conn)
        cmd.ExecuteNonQuery()
    End Using
End Sub

然后代码变成这样的

If Not aDataReader2.Read() Then
    CreateUser(user)
ElseIf min = 1 Then
    ...
    user.userID = max + 1
    CreateUser(user)
Else
    ...
    user.userID = min - 1
    CreateUser(user)
    lblResults.Text = "User Account successfully created"
    btnCreateUser.Enabled = False
End If

现在看起来好多了。

(我的代码未经过测试,只是为了给你一个想法。)


<强>更新

您无法像这样读取最小值和最大值。试试这样的事情

Dim min, max As Integer

Using conn As New OleDbConnection(aConnectionString)
    Dim cmd As OleDbCommand = _
        New OleDbCommand("SELECT MIN(UserID), MAX(UserID) FROM UserDetails", conn)
    conn.Open()
    Using reader As OleDbDataReader = cmd.ExecuteReader()
        reader.Read()
        If reader.IsDBNull(0) Then
            ' The table is empty
            min = 1
            max = 1
        Else
            min = reader.GetInt32(0)
            max = reader.GetInt32(1)
        End If
    End Using
End Using