2

我正在执行来自 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);
        }
4

0 回答 0