2012-08-24 31 views
2

我要tabels在Access數據庫更新2個tabels領域如何在同一時間

用戶

  • 用戶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 

所以我的問題是現在,我如何從用戶TABEL加場!? user.userid與userinfo.userinfoid中的數字相同。

編輯........編輯........編輯.......編輯.........編輯........ .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) 

回答

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

您好我用在WHERE userinfoid =」&會議( 「theeditid」)& 「」 話,那麼它就知道我需要的user.userid多數民衆贊成也許3!? –

+0

最後,我會添加一個註釋 – Fionnuala

+0

嗨,再次,所以編輯行下的代碼是OK!?或..... –

1

據瞭解,我認爲用戶和用戶信息表之間存在一對一的對應關係。 (考慮user.userid與userinfo.userinfoid相同,它們看起來是它們各自表中的主鍵)。所以,這兩個表應該合併成一個表。

現在,如果您想要更新兩個表中的列,您可以在每個表上運行兩個更新語句。如果你想確保原子性,你可以在一個事務中運行兩個更新語句。這是有道理的,還是我錯過了什麼?

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