插入SQL Server时,该列不属于表

时间:2019-06-27 10:16:31

标签: sql-server vb.net datatable

我正在尝试在两台服务器之间移动数据。我在每个服务器上都有一个相同的表,其中包含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块内。

1 个答案:

答案 0 :(得分:0)

好的,我设法解决了。 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)