【问题标题】:Column does not belong to table when Inserting to SQL Server插入到 SQL Server 时列不属于表
【发布时间】:2019-06-27 10:16:31
【问题描述】:

我正在尝试在两台服务器之间移动数据。我在每台服务器上都有一个相同的表,包含 3 个字段,Customer_Code(varchar)、Customer_EmailAddress(nvarchar)、Customer_EmailAddressPosted(bit)。

Dim DT_EmailsToUpdatePreCheck As New DataTable
Dim DT_EmailsToUpdateStage As New DataTable

我从 Server1 中选择所有数据并存储到 DT_EmailsToUpdatePreCheck

 TabletCommand.CommandText = "Select * from SOP_MissingCustomerEmail"
 DT_EmailsToUpdatePreCheck.Load(TabletCommand.ExecuteReader)
 TabletCommand.Parameters.Clear()

然后我遍历 DT_EmailsToUpdatePreCheck 并从 Server2 中获取 Customer_Code 和 Customer_EmailAddress 都不相同的记录数,并将数据存储到 DT_EmailsToUpdateStage 中。

For j As Integer = 0 To DT_EmailsToUpdatePreCheck.Rows.Count - 1

            ServerCommand.CommandText = "Select Count(*) from SOP_MissingCustomerEmail where Customer_Code <> @Customer_Code and Customer_EmailAddress <> @Customer_EmailAddress"
            ServerCommand.Parameters.AddWithValue("@Customer_Code", DT_EmailsToUpdatePreCheck.Rows(j).Item("Customer_Code"))
            ServerCommand.Parameters.AddWithValue("@Customer_EmailAddress", DT_EmailsToUpdatePreCheck.Rows(j).Item("Customer_EmailAddress"))

            If ServerCommand.ExecuteScalar = 0 Then

                DT_EmailsToUpdateStage.ImportRow(DT_EmailsToUpdatePreCheck.Rows(j))
            End If

            ServerCommand.Parameters.Clear()
Next

最后我遍历 DT_EmailsToUpdateStage 将记录插入到 Server2 中

  For i As Integer = 0 To DT_EmailsToUpdateStage.Rows.Count - 1
            ServerCommand.CommandText = "Insert Into SOP_MissingCustomerEmail2 (Customer_Code, Customer_EmailAddress, Customer_EmailAddressPosted) Values (@Customer_Code, @Customer_EmailAddress, @Customer_EmailAddressPosted)"
            ServerCommand.Parameters.AddWithValue("@Customer_Code", DT_EmailsToUpdateStage.Rows(i).Item("Customer_Code").ToString())
            ServerCommand.Parameters.AddWithValue("@Customer_EmailAddress", DT_EmailsToUpdateStage.Rows(i).Item("Customer_EmailAddress").ToString())
            ServerCommand.Parameters.AddWithValue("@Customer_EmailAddressPosted", DT_EmailsToUpdateStage.Rows(i).Item("Customer_EmailAddressPosted").ToString())

            MessageBox.Show("Row Inserted")
            ServerCommand.Parameters.Clear()
  Next

但在插入中我收到“列 'Customer_Code' 不属于表”错误。 注意:最后两个代码块在 Try-Catch 块内。

【问题讨论】:

  • 您是否尝试过在调试器中单步执行第二个循环?哪一行引发了异常?

标签: sql-server vb.net datatable


【解决方案1】:

好的,我设法解决了。未正确填充 DT_EmailsToUpdateStage。创建数据表时,我必须指定列结构,以便在按列名调用项目时,可以找到它。

    Dim DT_UpdateEmailStage1 As New DataTable
    DT_UpdateEmailStage1.Columns.Add("Customer_Code", Type.GetType("System.String"))
    DT_UpdateEmailStage1.Columns.Add("Customer_EmailAddress", Type.GetType("System.String"))
    DT_UpdateEmailStage1.Columns.Add("Customer_EmailAddressPosted", Type.GetType("System.String"))

    Dim DT_UpdateEmailStage2 As New DataTable
    DT_UpdateEmailStage2.Columns.Add("Customer_Code", Type.GetType("System.String"))
    DT_UpdateEmailStage2.Columns.Add("Customer_EmailAddress", Type.GetType("System.String"))
    DT_UpdateEmailStage2.Columns.Add("Customer_EmailAddressPosted", Type.GetType("System.String"))

我还更改了将值添加到行的方式。首先,我将表中的值放入一个变量中,然后将它们作为参数传递给 Rows.Add() 而不是使用 ImportRow()

            Dim code As String
            Dim email As String
            Dim posted As String

            code = DT_UpdateEmailStage1.Rows(j).Item(0)
            email = DT_UpdateEmailStage1.Rows(j).Item(1)
            posted = DT_UpdateEmailStage1.Rows(j).Item(2)

            If ServerCommand.ExecuteScalar = 0 Then
                DT_UpdateEmailStage2.Rows.Add(code, email, posted)
            End If

和进入Server2时类似的方法

            Dim code As String
            Dim email As String
            Dim posted As String

            code = DT_UpdateEmailStage2.Rows(i).Item(0)
            email = DT_UpdateEmailStage2.Rows(i).Item(1)
            posted = DT_UpdateEmailStage2.Rows(i).Item(2)


            ServerCommand.CommandText = "insert into SOP_MissingCustomerEmail(customer_code, customer_emailaddress, customer_emailaddressposted) values (@customer_code, @customer_Emailaddress, @customer_EmailaddressPosted)"
            ServerCommand.Parameters.AddWithValue("@customer_code", code)
            ServerCommand.Parameters.AddWithValue("@customer_emailaddress", email)
            ServerCommand.Parameters.AddWithValue("@customer_emailaddressposted", posted)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多