如何在同一时间更新2个表中的字段

时间:2012-08-24 09:43:39

标签: asp.net database ms-access ado.net sql-update

我必须在访问数据库中使用表格

用户

  • 用户ID
  • 用户名
  • useractiv

USERINFO

  • userinfoid
  • userrealname
  • USERPHONE

然后我从一些asp:文本字段获取信息到更新处理程序,我知道如何为一个表而不是2表。

通常我会用这个

    Dim strSQL As String = ""
    strSQL = "" & _
    "UPDATE Userinfo " & _
    "SET userrealname = @therealname, userphone = @theuserphone " & _
    "WHERE userinfoid =" & Session("theeditid") & ""

    Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
        Using updatecmd As OleDbCommand = New OleDbCommand(strSQL, connection)
            updatecmd.CommandType = CommandType.Text
            updatecmd.Parameters.AddWithValue("@therealname", OleDbType.VarChar).Value = TextBox1.Text
            updatecmd.Parameters.AddWithValue("@theuserphone", OleDbType.VarChar).Value = CKEditor1.Text
            Try
                updatecmd.Connection.Open()
                Dim i As Integer = CInt(updatecmd.ExecuteNonQuery())
                If i = 0 Then
                    Session("editsucces") = "NoMatch" 'no rows were updated because none matched the criteria
                End If
            Catch ex As Exception
                Session("editsucces") = "DBerror" 'Something went wrong, such as the database was unavailable
            End Try
        End Using
    End Using

所以现在我的问题是,如何添加用户表格中的字段! user.userid与userinfo.userinfoid中的编号相同。

EDIT ........ EDIT ........ EDIT ....... EDIT .......... EDIT ......... EDIT .......... EDIT

所以这段代码还可以,或者你重新修改我改变了一些代码!?

Dim strSQL As String = ""
    strSQL = "" & _
    "UPDATE Users INNER JOIN Userinfo ON Users.UserID = Userinfo.UserID " & _
    "SET Users.Username = [@uname], Users.Password = [@upass], Users.UserActiv = [@uactiv], Userinfo.UserRealName = [@urname], Userinfo.UserEmail1 = [@umail], Userinfo.UserDOB = [@udob], Userinfo.UserPhone1 = [@uphone1], Userinfo.UserPhone2 = [@uphone2], Userinfo.UserPhone3 = [@uphone3], Userinfo.UserYear = [@uyear], Userinfo.UserSick = [@usick] " & _
    "WHERE Users.UserID = [@uid]"

    Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
        Using updatecmd As OleDbCommand = New OleDbCommand(strSQL, connection)
            updatecmd.CommandType = CommandType.Text
            updatecmd.Parameters.AddWithValue("@uname", OleDbType.VarChar).Value = userinput1.Text
            updatecmd.Parameters.AddWithValue("@upass", OleDbType.VarChar).Value = userinput2.Text
            updatecmd.Parameters.AddWithValue("@uactiv", OleDbType.VarChar).Value = "Y"
            updatecmd.Parameters.AddWithValue("@urname", OleDbType.VarChar).Value = userinput3.Text
            updatecmd.Parameters.AddWithValue("@umail", OleDbType.VarChar).Value = userinput4.Text
            updatecmd.Parameters.AddWithValue("@udob", OleDbType.VarChar).Value = userinput5.Text
            updatecmd.Parameters.AddWithValue("@uphone1", OleDbType.VarChar).Value = userinput6.Text
            updatecmd.Parameters.AddWithValue("@uphone2", OleDbType.VarChar).Value = userinput7.Text
            updatecmd.Parameters.AddWithValue("@uphone3", OleDbType.VarChar).Value = userinput8.Text
            updatecmd.Parameters.AddWithValue("@uyear", OleDbType.VarChar).Value = userinput9.Text
            updatecmd.Parameters.AddWithValue("@usick", OleDbType.VarChar).Value = usertextarea.Text
            Try
                updatecmd.Connection.Open()
                Dim i As Integer = CInt(updatecmd.ExecuteNonQuery())
                If i = 0 Then
                    Session("editsucces") = "NoMatch" 'no rows were updated because none matched the criteria
                End If
            Catch ex As Exception
                Session("editsucces") = "DBerror" 'Something went wrong, such as the database was unavailable
            End Try
        End Using
    End Using

    Response.Redirect("default.aspx", False)

3 个答案:

答案 0 :(得分:1)

据我所知,我认为user和userinfo表之间存在一对一的对应关系。 (考虑到user.userid与userinfo.userinfoid相同,它们似乎是各自表中的主键)。因此,这两个表应该合并为一个表。

现在,如果要更新两个表中的列,则每个表上将运行两个update语句。如果要确保原子性,可以在单个事务中运行两个更新语句。这有意义还是我错过了什么?

答案 1 :(得分:1)

应该可以创建一个连接来更新两个表。例如:

sSQL = "UPDATE [User] INNER JOIN userinfo " _
& "ON User.UserID = userinfo.userinfoid SET " _
& "[User].UserName = [uname], userinfo.userrealname = [urname] " _
& "WHERE [User].UserID = [uid]"

cmd.ActiveConnection = cn
cmd.CommandText = sSQL
cmd.CommandType = adCmdText
cmd.Parameters.Append cmd.CreateParameter( _
    "uname", adVarChar, adParamInput, 50, "user name")
cmd.Parameters.Append cmd.CreateParameter( _
    "urname", adVarChar, adParamInput, 50, "user real name")
cmd.Parameters.Append cmd.CreateParameter( _
    "uid", adInteger, adParamInput, , 2)
cmd.Execute

答案 2 :(得分:1)

你应该使用交易。通过原子方式对数据库进行许多更改是常见的方法。 您的案例的好例子http://www.dreamincode.net/forums/topic/186402-transaction-on-access-database-adonet/

Using con As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
  Dim cmd As New OleDbCommand("", con)
  Dim tra As OleDbTransaction = Nothing
  Dim ct1 As String = ""
  Dim ct2 As String = ""

  Try
    con.Open()
    tra = con.BeginTransaction
    cmd.Transaction = tra

    strSQL = "" & _
    "UPDATE User " & _
    ' set fields needed to update in table USER
    "WHERE userid =" & Session("theeditid") & ""
    cmd.CommandText = strSQL
    ' set parameters for user update
    cmd.ExecuteNonQuery()

    strSQL = "" & _
    "UPDATE Userinfo " & _
    "SET userrealname = @therealname, userphone = @theuserphone " & _
    "WHERE userinfoid =" & Session("theeditid") & ""
    cmd.CommandText = strSQL
    cmd.Parameters.Clear()

    ' set parameters for userinfo update
    cmd.ExecuteNonQuery()

    ' Complete transacton
    tra.Commit()

    ' i think here should be closing cmd

    Catch ex As Exception
      MsgBox("The data could not be saved.", MsgBoxStyle.Critical, "Error")
      Try : tra.Rollback() : Catch : End Try
    End Try
End Using