2012-12-02 69 views
0

我對ASP.net真的很陌生,我目前正在嘗試在網站上創建一個註冊頁面。我成功地向數據庫添加了用戶,但我決定在代碼中添加另一個功能以檢查哪些用戶ID可用。例如,如果用戶刪除了他們的帳戶,他們的用戶ID將可以再次使用。我試圖找到最小值和最大值,並根據它是最小值還是最大值來加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對他們倆的 - 當最小應爲1和最大應爲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" 
+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+2

重新使用舊的ID不是可以推薦的。 – adripanico

+0

謝謝約翰。 Adripanico我也不會這麼想,但是這是我的講師向班級推薦的,以獲得A等級的信息 – user1850189

回答

1

這很難說是什麼 t時的問題正好,因爲我們看到的只是它的一部分。 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 

這看起來更漂亮了。

(我的代碼沒有經過測試,它只是給你一個想法。)


UPDATE

您無法讀取最小值和最大值是這樣的。嘗試像這樣

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 
+0

謝謝奧利弗。我從字面上只開始學習ASP 4周前,我們還沒有顯示如何編寫代碼 – user1850189

+0

非常感謝你奧利弗:)我得到了你的建議 – user1850189

+0

-1(沒有投票,只是通過評論)工作的sql注入易受攻擊代碼。 –

相關問題