SQL语句之后数据库没有更新

SQL语句之后数据库没有更新

本文介绍了在ASP.net UPDATE SQL语句之后数据库没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个问题attepting更新我的数据库中的记录。我有一个显示在文本框中用户的详细信息,这些信息是从会话在登录后进入的网页。这样做的目的是当用户将覆盖在文本框中当前文本更新的详细信息。

I currently have a problem attepting to update a record within my database. I have a webpage that displays in text boxes a users details, these details are taken from the session upon login. The aim is to update the details when the user overwrites the current text in the text boxes.

我有当用户点击了保存详细信息按钮,它似乎工作,因为我已经测试了受影响的行数,它输出1。但是运行的功能,检查数据库时,记录还没有被更新,而且我不确定,为什么。

I have a function that runs when the user clicks the 'Save Details' button and it appears to work, as i have tested for number of rows affected and it outputs 1. However, when checking the database, the record has not been updated and I am unsure as to why.

我已经检查了正在由显示它作为一个标签处理的SQL语句,它看起来像这样:

I've have checked the SQL statement that is being processed by displaying it as a label and it looks as so:

UPDATE [users]
   SET [email] = @email,
       [firstname] = @firstname,
       [lastname] = @lastname,
       [promo] = @promo
 WHERE [users].[user_id] = 16

功能和其它相关code是:

The function and other relevant code is:

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

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

您的网页是否有RequiresTransaction的财产?如果是这样,检查是否有任何可能导致事务回滚请求期间其他地方抛出的异常 - 离开数据不变

Does your page have a RequiresTransaction property? If so, check that there are no exceptions thrown elsewhere during the request which might cause the transaction to roll back - leaving the data unchanged.

这篇关于在ASP.net UPDATE SQL语句之后数据库没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 23:05