我對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"
我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –
重新使用舊的ID不是可以推薦的。 – adripanico
謝謝約翰。 Adripanico我也不會這麼想,但是這是我的講師向班級推薦的,以獲得A等級的信息 – user1850189