我目前在注册更新数据库中的记录时遇到问题。我有一个网页,在文本框中显示用户详细信息,这些详细信息是在登录时从会话中获取的。目的是在用户覆盖文本框中的当前文本时更新详细信息。
我有一个在用户单击“保存详细信息”按钮时运行的功能,它似乎正常工作,因为我已经测试了受影响的行数并输出1.但是,在检查数据库时,记录没有已更新,我不确定为什么。
我已经通过将其显示为标签来检查正在处理的SQL语句,它看起来如此:
UPDATE [users]
SET [email] = @email,
[firstname] = @firstname,
[lastname] = @lastname,
[promo] = @promo
WHERE [users].[user_id] = 16
该功能和其他相关代码是:
Sub Page_Load(sender As Object, e As EventArgs)
usernameLabel.text = session.contents.item("UserName")
if usernameLabel.text = "" then
logoutButton.Visible = False
loggedInAsLabel.Visible = False
else
labelGuest.Visible = False
linkLogin.Visible = False
linkRegister.Visible = False
end if
emailBox.text = session.contents.item("Email")
firstBox.text = session.contents.item("FirstName")
lastBox.text = session.contents.item("LastName")
promoBox.text = session.contents.item("Promo")
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
changeDetails(emailBox.text, firstBox.text, lastBox.text, promoBox.text)
End Sub
Function changeDetails(ByVal email As String, ByVal firstname As String, ByVal lastname As String, ByVal promo As String) As Integer
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Documents an"& _
"d Settings\Paul Jarratt\My Documents\ticketoffice\datab\ticketoffice.mdb"
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
Dim queryString As String = "UPDATE [users] SET [email]=@email, [firstname]=@firstname, [lastname]=@lastname, "& _
"[promo]=@promo WHERE ([users].[user_id] = " + session.contents.item("ID") + ")"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dbParam_email As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_email.ParameterName = "@email"
dbParam_email.Value = email
dbParam_email.DbType = System.Data.DbType.[String]
dbCommand.Parameters.Add(dbParam_email)
Dim dbParam_firstname As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_firstname.ParameterName = "@firstname"
dbParam_firstname.Value = firstname
dbParam_firstname.DbType = System.Data.DbType.[String]
dbCommand.Parameters.Add(dbParam_firstname)
Dim dbParam_lastname As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_lastname.ParameterName = "@lastname"
dbParam_lastname.Value = lastname
dbParam_lastname.DbType = System.Data.DbType.[String]
dbCommand.Parameters.Add(dbParam_lastname)
Dim dbParam_promo As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_promo.ParameterName = "@promo"
dbParam_promo.Value = promo
dbParam_promo.DbType = System.Data.DbType.[String]
dbCommand.Parameters.Add(dbParam_promo)
Dim rowsAffected As Integer = 0
dbConnection.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try
labelTest.text = rowsAffected.ToString()
if rowsAffected = 1 then
labelSuccess.text = "* Your details have been updated and saved"
else
labelError.text = "* Your details could not be updated"
end if
End Function
非常感谢任何帮助。
答案 0 :(得分:0)
您的网页是否有RequiresTransaction属性?如果是这样,请检查请求期间在其他地方没有抛出异常,这可能导致事务回滚 - 保持数据不变。
答案 1 :(得分:0)
您确定要更新您认为正在更新的数据库吗?我根本不喜欢那个连接字符串的外观(坦率地说,我怀疑Paul Jarratt会很高兴看到它在这里发布)。