简体   繁体   English

SQL批量+存储过程

[英]SQL bulk + stored procedure

Error: The given ColumnMapping does not match up with any column in the source or destination. 错误:给定的ColumnMapping与源或目标中的任何列都不匹配。 Does some one have a idea on how to resolve this issue? 有人对如何解决此问题有想法吗? Is it giving the error because i'm trying to get my TreinId by treinNaam, which is stored in a different table? 是否由于我试图通过存储在不同表中的treinNaam获取TreinId而给出错误? or? 要么? i'm using SQL server management studio 2008 我正在使用SQL Server Management Studio 2008

Any help is much appreciated! 任何帮助深表感谢!

public void BatchBulkCopy(string rapport)
        {
            // Get the DataTable 
            DataTable dtInsertRows = TextReader(rapport);

            using (SqlBulkCopy sbc = new SqlBulkCopy(GetConnectionString(), SqlBulkCopyOptions.KeepIdentity))
            {
                sbc.DestinationTableName = "dbo.Fouten";
                sbc.BatchSize = 2000;


                sbc.ColumnMappings.Add("Datum", "Datum");
                sbc.ColumnMappings.Add("Foutcode", "FoutCode");
                sbc.ColumnMappings.Add("Omschrijving", "Omschrijving");
                sbc.ColumnMappings.Add("Module", "Module");
                sbc.ColumnMappings.Add("Time", "Time");
                sbc.ColumnMappings.Add("Teller", "Teller");
                sbc.ColumnMappings.Add("Mnemo", "Mnemo");
                sbc.ColumnMappings.Add("treinNaam", "treinNaam");

                sbc.NotifyAfter = dtInsertRows.Rows.Count;
                sbc.SqlRowsCopied += new SqlRowsCopiedEventHandler(sbc_SqlRowsCopied);

                sbc.WriteToServer(dtInsertRows);
                sbc.Close();
            }

        }

        void sbc_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
        {
            throw new NotImplementedException();
        }

Datatable filed by: 资料表提出者:

dt.Rows.Add(new object[] { datum, foutcode, omschrijving, test[test.Length - 1],tijd,teller,absentOfPresent,TreinNaam });
return dt;

SQL stored procedure: SQL存储过程:

USE [Events]
GO
/****** Object:  StoredProcedure [dbo].[sp_BatchInsert]    Script Date: 03/15/2016 09:13:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_BatchInsert] (@Datum date, @Foutcode varchar(8), @Omschrijving varchar(50), @Module varchar(20), @Time time(3), @Teller int, @Mnemo varchar(9), @treinNaam int)
AS
BEGIN
            INSERT INTO dbo.Fouten VALUES (@Datum,@Foutcode,@Omschrijving,@Module,@Time,@Teller,@Mnemo,(SELECT TreinId from [Events].[dbo].[Treinen] WHERE Name = @treinNaam));
END 

Database design: 数据库设计:

在此处输入图片说明

在此处输入图片说明

From DBMS perspective TreinNaam doesn't exist in this context. 从DBMS角度来看,TreinNaam在这种情况下不存在。 It has to be column available in Fouten. 它必须在Fouten中可用。

You have 2 choices here: 您有2种选择:

  1. Find all treinId in advance and replace treinNaam with treinId 预先查找所有treinId并将treinNaam替换为treinId
  2. Create staging table and dump data as you have, then write stored proc which will take care of moving data into Fouten and find all required connections 创建临时表并按需转储数据,然后编写存储的proc,该proc将负责将数据移至Fouten并查找所有必需的连接

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

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