简体   繁体   English

使用dataadapter.update时列是否需要完全匹配

[英]Do columns need to match exactly when using dataadapter.update

I'm working on a project to import data from a Microsoft Access database into a blank MS-SQL database. 我正在一个项目中,将数据从Microsoft Access数据库导入空白MS-SQL数据库。 The structure of the table in Access (VM) does not match up exactly with the table in MS-SQL (vlVerticalMarketing). Access(VM)中表的结构与MS-SQL(vlVerticalMarketing)中的表不完全匹配。

AccessTable VM                SQLTable vlVerticalMarketing                                
ID (autonumber)               VMID (PK, identity)
VM                            VerticalMarketing
                              Deleted (required)
                              Archive (required)
                              Other non-required fields

I'm filling the Access data into a DataTable DT using the following query: 我使用以下查询将Access数据填充到DataTable DT中:

select VM as VerticalMarketing, 0 as Deleted, 0 as Archive from VM where VM is not null

Then I change the name of the DataTable using: 然后,我使用以下方式更改数据表的名称:

DT.TableName = "vlVerticalMarketing"

From there, I pass the DataTable into a class function. 从那里,我将DataTable传递给一个类函数。

Public Function CopyFromDataTable(ByVal sourceDT As DataTable, ByVal TableName As String) As Boolean
    Try
        OpenDB()
        Dim DA As New SqlDataAdapter("select VerticalMarketing, Deleted, Archive from " & TableName, Me.DBConn)
        Dim CB = New SqlCommandBuilder(DA)
        CB.QuotePrefix = "["
        CB.QuoteSuffix = "]"
        DA.InsertCommand = CB.GetInsertCommand(True)
        Dim destDT = sourceDT.Copy()
        DA.Update(destDT)

OpenDB is a class method that I use for other things and I know is good. OpenDB是我用于其他用途的类方法,我知道这是很好的。 It sets the connection string and creates DBConn as a SqlConnection object. 它设置连接字符串,并将DBConn创建为SqlConnection对象。 The rest is standard VB.Net. 其余的是标准的VB.Net。 Everything runs without error. 一切正常运行。 Both sourceDT and destDT have data in them (viewed through the DataSet Visualizer while debugging). sourceDT和destDT都具有数据(在调试时通过DataSet Visualizer查看)。 The problem is that no data gets passed out to the SQL database. 问题是没有数据传递到SQL数据库。

Any ideas on what I'm doing wrong that is causing the SQL database not to receive the data. 关于我做错了什么的任何想法都导致SQL数据库无法接收数据。

i think the problem is that the rows in destDt don't have RowState.Added when you call Update on the DataAdapter ... you can check the return value of the Update statement ... i guess it will be 0 because no inserts were executed ... you could check this with sql server profiler ... 我认为问题是destDt中的行没有RowState.Added,当您在DataAdapter上调用Update时...您可以检查Update语句的返回值...我想它将是0,因为没有插入执行...您可以使用sql server profiler检查此问题...

as a workaround for this you could call DataRow.SetAdded() on the rows in your destDT ... 作为解决方法,您可以在destDT中的行上调用DataRow.SetAdded()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM