2

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

用户

  • 用户身份
  • 用户名
  • 用户激活

用户信息

  • 用户信息
  • 用户实名
  • 用户电话

然后我从一些 asp:text 字段获取信息到更新处理程序,我知道如何为一个表执行此操作,但不是为 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 中的数字相同。

编辑…………编辑…………编辑…………编辑…………编辑…………编辑…… ........编辑

所以这段代码没问题,还是你建议我改变一些!?

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)
4

3 回答 3

1

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

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

于 2012-08-24T09:50:03.960 回答
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
于 2012-08-24T09:58:54.837 回答
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
于 2012-08-24T14:29:11.360 回答