SqlBulkCopy WriteToServer方法throw无法访问目标表'[#tablename]'

时间:2012-06-21 22:28:41

标签: asp.net-mvc sqlbulkcopy

我正在执行c#MVC3中的以下代码,但在bulkCopy.WriteToServer上出现错误“无法访问目标表'[#tablename]'

任何人都知道下面的代码有什么问题。我去了一些解决方案,但在这里不起作用。

var uniqueID = Guid.NewGuid();
        AsclipeiousMembercode = AsclipeiousMembercode.Distinct().ToList();

        System.Data.DataTable datatable = new System.Data.DataTable();
        datatable.Columns.Add("Guid", uniqueID.GetType());
        datatable.Columns.Add("MemberCode");
        datatable.Columns.Add("DateCreated", DateTime.Now.GetType());

        var connectionString = Aurora.Customers.Data.Properties.Settings.Default.AuroraCustomerDBConnectionString;
        using (System.Data.SqlClient.SqlBulkCopy bulkCopy =
        new System.Data.SqlClient.SqlBulkCopy(connectionString, System.Data.SqlClient.SqlBulkCopyOptions.KeepIdentity))
        {
            var tableName = "[#MemberCode_TempTable_" + uniqueID.ToString().Replace("-", "") + "]";                
            var commandText = "CREATE TABLE " + tableName + " ([id] [int] IDENTITY(1,1) NOT NULL,[Guid] [uniqueidentifier] NOT NULL,[MemberCode] [nvarchar](max) NOT NULL,  [DateCreated] [datetime] NOT NULL)";

            Db.ExecuteCommand(commandText);
            Db.SubmitChanges();

            bulkCopy.DestinationTableName = tableName;
            bulkCopy.ColumnMappings.Add("Guid", "Guid");
            bulkCopy.ColumnMappings.Add("MemberCode", "MemberCode");
            bulkCopy.ColumnMappings.Add("DateCreated", "DateCreated");

            datatable = dbMemberCodeTempTable.ToDataTable();
            bulkCopy.WriteToServer(datatable);
        }

0 个答案:

没有答案