当前位置:编程学习 > C#/ASP.NET >>

如何将两个 DataTable 合并并且更新到数据库


目前我在做一个系统,系统是由两个子系统:本部和分部组成。
现在问题是这样,本部和分部各有一个相同的SQL数据表,这个表只能在本部系统修改保存。

随后,分部系统手动点击更新,更新处理过程很简单,就是把本部的数据照搬过来。但是我用下面的语句更新后,没有提示错误,但是分部的数据库却没变,大家看看是什么原因,在线等,急,谢谢!

注:InsertCommand、DeleteCommand、UpdateCommand命令已经正确生成,使用单表能够正常更新,不是这个原因.

Public Shared Sub BeginUpdate()
        ' 同步更新 ProductKindList 表
        Dim centerProductKindDap As New SqlDataAdapter("GetProductKindList", SysInfo.GetCenterConnection)
        centerProductKindDap.SelectCommand.CommandType = CommandType.StoredProcedure
        Dim centerProductKindCmdBdr As New SqlCommandBuilder(centerProductKindDap)

        Dim agentProductKindDap As New SqlDataAdapter("GetProductKindList", SysInfo.GetAgentConnection)
        agentProductKindDap.SelectCommand.CommandType = CommandType.StoredProcedure
        Dim agentProductKindCmdBdr As New SqlCommandBuilder(agentProductKindDap)

        Dim centerProductKindTable As New DataTable

        Dim agentProductKindTable As New DataTable

        Dim newProductKindTable As New DataTable

        Try
            centerProductKindDap.Fill(centerProductKindTable)
        Catch ex As Exception
            Throw New Exception("远程数据库连接失败!" & vbCrLf & ex.Message)
        End Try

        Try
            agentProductKindDap.Fill(agentProductKindTable)
        Catch ex As Exception
            Throw New Exception("本地数据库连接失败!" & vbCrLf & ex.Message)
        End Try

        agentProductKindTable.Merge(centerProductKindTable, False, MissingSchemaAction.AddWithKey)

        agentProductKindDap.Update(agentProductKindTable)
End Sub --------------------编程问答-------------------- 试了很久,还是不行,高手求助~!! --------------------编程问答-------------------- 贮存过程中有没有错误?

New SqlCommandBuilder(贮存过程)是否产生了更新的command?

--------------------编程问答-------------------- New SqlCommandBuilder(贮存过程)是否产生了更新的command?

这个完全正常,如果是单表的话,更新完全没问题 --------------------编程问答-------------------- 问题解决了,不过应该不是最好的方法,我的方法如下:

1、定义一个删除数据行的存储过程 DelRow_ProductKind
2、将两个 DataTable 手动添加主键
3、合并两个 DataTable 并且将每一行的 RowState 设置为 Added
4、合并后的 DataTable ,将数据库中相应的数据行删掉(调用前面的存储过程)

代码如下:


Public Shared Sub BeginUpdate()
        Dim sqlCmd As SqlCommand = New SqlCommand("DelRow_ProductKind", SysInfo.GetAgentConnection)
        sqlCmd.CommandType = CommandType.StoredProcedure
        sqlCmd.Parameters.Add("@ID", SqlDbType.VarChar)

        ' 同步更新 ProductKindList 表
        Dim centerProductKindDap As New SqlDataAdapter("GetProductKindList", SysInfo.GetCenterConnection)
        centerProductKindDap.SelectCommand.CommandType = CommandType.StoredProcedure
        Dim centerProductKindCmdBdr As New SqlCommandBuilder(centerProductKindDap)

        Dim agentProductKindDap As New SqlDataAdapter("GetProductKindList", SysInfo.GetAgentConnection)
        agentProductKindDap.SelectCommand.CommandType = CommandType.StoredProcedure
        Dim agentProductKindCmdBdr As New SqlCommandBuilder(agentProductKindDap)

        Dim centerProductKindTable As New DataTable

        Dim agentProductKindTable As New DataTable

        Dim newProductKindTable As New DataTable

        Try
            centerProductKindDap.Fill(centerProductKindTable)
            centerProductKindTable.PrimaryKey = New DataColumn() {centerProductKindTable.Columns("ID")}
        Catch ex As Exception
            Throw New Exception("远程数据库连接失败!" & vbCrLf & ex.Message)
        End Try

        Try
            agentProductKindDap.Fill(agentProductKindTable)
            agentProductKindTable.PrimaryKey = New DataColumn() {agentProductKindTable.Columns("ID")}
        Catch ex As Exception
            Throw New Exception("本地数据库连接失败!" & vbCrLf & ex.Message)
        End Try

        agentProductKindTable.Merge(centerProductKindTable, False)

        For Each tmpRow As DataRow In agentProductKindTable.Rows
            sqlCmd.Parameters("@ID").SqlValue = tmpRow("ID").ToString
            sqlCmd.Connection.Open()
            sqlCmd.ExecuteNonQuery()
            sqlCmd.Connection.Close()
            tmpRow.SetAdded()
        Next

        agentProductKindDap.Update(agentProductKindTable)
End Sub 

各位高手如果有更好的解决方法,欢迎赐教~!
--------------------编程问答-------------------- 上面的步骤补充一下:

5、将合并后的 DataTable 更新到数据库中

--------------------编程问答-------------------- 另建一张临时表,内容一样 --------------------编程问答-------------------- 楼主,合并后的DataTable表能够更新数据库吗?
我试着好像更新不到数据库中啊。。
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,